class Test {
constructor(fn) {
fn(this._resolve);
}
_resolve(value) {
console.log(this);
}
}
let p = new Test(function (resolve) {
resolve('5秒');
})
为什么输出this输出为 undefined 运行环境不能是undefined吧
js里的this一般分为四种情况
1.在全局环境下调用,如foo(),this指向window,严格模式下为undefinded
2.被对象调用,如a.foo(),this指向a
3.通过call,apply,bind等函数强制绑定this
4.var foo=new function Foo();这个时候this指向foo,即Foo()构造出来的对象。
如果能帮助到你,我也很高兴。
ps:我说的仅作为参考,不是金科玉律。