var x=3; var foo={ x:2, //2 baz:{ x:1, //1 bar:function(){ return this.x; //1 } } } var go=foo.baz.bar; console.log(go()+foo.baz.bar())
var x = 3;
var foo = {
x: 2,
baz: {
x: 1,
bar: function() {
return this.x;
}
}
}
//here you save a function expression
//to a variablewith name go
var go = foo.baz.bar;
//you execute go but this refer to the global object
//and x has the value of 3 in the global object
console.log(go());//this will output 3
//this refer to the object foo where x has
//the value of 1
console.log(foo.baz.bar());//this will output 3
Run code snippet