var a = "outer";
let obj = {
a: "inner",
foo: () => {
console.log(this.a);
},
};
obj.foo();
class Foo {
print = () => console.log(this.x);
constructor() {
this.x = 1;
}
}
let foo = new Foo();
foo.print.call({ x: 2 });
// 箭头函数与this - 代码段3
function printThis() {
let print = () => console.log(this);
print();
}
printThis.call([1]);
printThis.call([2]);
var _name = "global";
var obj = {
func() {
const innerFunc = () => console.log(this._name);
return innerFunc;
},
_name: "local",
};
// 语句1
obj.func()(); // var fn = obj.func() this->obj local
// 语句2
var func = obj.func;
func()(); // this-> window global
// 语句3
obj.func.bind({ _name: "newObj" })()(); // let fn = obj.func.bind({ _name: "newObj" })() newObj
// 语句4
obj.func.bind()()(); // window global
this面试题
最新推荐文章于 2024-10-11 19:03:40 发布