Object variable scope javascript? - Stack Overflow

I don't understand how this scope works. How does the value of eg.i get modified in first when it

I don't understand how this scope works. How does the value of eg.i get modified in first when it is only modified in second?

EXAMPLE

var obj = {

    first: function() {

        var eg = {i: 0}; // eg equals 0 here

        obj.second(eg);
        obj.second(eg);

        console.log(eg.i); // 2
    },

    second: function(eg) {

        ++eg.i;
    }
};

How does eg.i get modified in the first function as well?

I don't understand how this scope works. How does the value of eg.i get modified in first when it is only modified in second?

EXAMPLE

var obj = {

    first: function() {

        var eg = {i: 0}; // eg equals 0 here

        obj.second(eg);
        obj.second(eg);

        console.log(eg.i); // 2
    },

    second: function(eg) {

        ++eg.i;
    }
};

How does eg.i get modified in the first function as well?

Share Improve this question asked Jun 2, 2013 at 20:07 user2251919user2251919 6752 gold badges11 silver badges23 bronze badges 2
  • 2 It's passed by reference into obj.second(). – Michael Berkowski Commented Jun 2, 2013 at 20:10
  • This answer may help a little bit. stackoverflow./questions/518000/… – AlexLordThorsen Commented Jun 2, 2013 at 20:11
Add a ment  | 

3 Answers 3

Reset to default 4

When objects are passed in to functions in JS, they're passed in as references rather than values: http://snook.ca/archives/javascript/javascript_pass

As noted in my ment to Ahmed Nuaman's answer, saying that the argument is passed by reference is incorrect. Proving that is trivial:

function change (x) {
    //"replace" x with a different object?
    x = {
        a : 4
    };
}
var o = { a : 6 };
change(o);
console.log(o.a); //6

If the argument would truly have been passed by reference, o.a would have been 4. However, the argument is passed by the reference value - in this case, the value is simply and object.

And that's why you observed the symptoms presented in the question: When you directly modified the argument, the object passed in was changed.

It's as if you got some ice-cream, then went back to the stand to ask for an extra scoop of vanilla. You still have the same cup, but with that extra something. Using that analogy in the example above, you ask for a new vanilla ice-cream: you still have the old one, and that hasn't changed (well, it may have melted a bit). In a pass-by-reference scenario, you would've dumped your existing ice-cream, and got a brand new one.

As a final note: This is not about scoping rules in js. If eg wasn't passed to the second function, it would've been unavailable to it.

You're passing eg as a parameter to second. It might be easier to see if you renamed it:

second: function(x) {
    ++x.i;
}

It gets modified in the first function because you modify it with the second function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743871940a4521604.html

相关推荐

  • Object variable scope javascript? - Stack Overflow

    I don't understand how this scope works. How does the value of eg.i get modified in first when it

    3天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信