this
function foo() {
console.log(this);
}
var obj1 = {
name: 'obj1',
foo: foo,
};
var obj2 = {
name: 'obj2',
obj1: obj1,
};
obj2.obj1.foo();
function foo() {
console.log(this);
}
var obj1 = {
name: 'obj1',
foo: foo,
};
var bar = obj1.foo;
bar();
var obj = {
bar: function () {
var x = () => this;
return x;
},
};
var fn = obj.bar();
console.log(fn() === obj);
var fn2 = obj.bar;
console.log(fn2()() == window);
var name = 'window';
var obj = {
name: 'hello',
getName() {
console.log(this.name);
},
};
function getName() {
var objGetName = obj.getName;
objGetName();
obj.getName();
obj.getName();
(true && obj.getName)();
}
getName();
var name = 'window';
var obj = {
name: 'obj',
getName1() {
console.log(this.name);
},
getName2: () => console.log(this.name),
getName3() {
return function () {
console.log(this.name);
};
},
getName4() {
return () => {
console.log(this.name);
};
},
};
var obj2 = { name: 'obj2' };
obj.getName1(); // obj
obj.getName1.call(obj2); // obj2
obj.getName2(); // window
obj.getName2.call(obj2); // obj2 xxxxxxxxxxxxxxxxxxxxxxx window
obj.getName3()(); // window
obj.getName3.call(obj2)(); // window
obj.getName3().call(obj2); // obj2
obj.getName4()(); // obj
obj.getName4.call(obj2)(); // obj2
obj.getName4().call(obj2); // obj2 xxxxxxxxxxxxxxxxxxxxxxx obj