see this piece of code, as follows:
function foo() {
console.log( this.a );
}
var obj2 = {
a: 42,
foo: foo
};
var obj1 = {
a: 2,
obj2: obj2
};
obj1.obj2.foo(); //42
how should I understand this code?
as I understand it, obj2 should end up with:
var obj2 = {
a: 42,
foo: function () {
console.log( this.a );
}
};
var obj1 = {
a: 2,
foo: {
a: 42,
obj2: function () {
console.log( this.a );
}
}
};
if this is the case, I really don"t understand the chain writing of obj1.obj2.foo ();
.
also hopes to give me some advice. Thank you!