Could someone help me with this function query:
var MyObject3 = function (a, b) {
var obj = { myA : a, myB : b } ;
obj.foo = function () { return obj.myA + obj.myB ; } ;
obj.bar = function (c) { return obj.myA + c ; } ;
return obj ;
} ;
I understand that obj.foo and obj.bar are closures. I started by executing:
obj3 = MyObject3(1, 2) ;
The outcome was: {"myA" :1,"myB" :2}
, which is expected. What confuses me is the following: if I change the value of obj3.myA to 4 with; > obj3.myA = 4 ; obj3 ; the result becomes: {"myA" :4,"myB" :2}
. However, when I try > obj3.foo() ;
, it returns 6. Shouldn't obj3.foo() be a closure resulting in 3 instead of 6?