Skip to main content

这段代码中的 this 是多少?

把判断依据背下来才能全对

var length = 4
function callback() {
console.log(this.length) // => 打印出什么?
}

const obj = {
length: 5,
method(callback) {
callback()
}
}

obj.method(callback, 1, 2)
//4

建议熟读这篇文章:

this 的值到底是什么?一次说清楚 举例法

1.  fn()
//this => window/global
2. obj.fn()
//this => obj
3. fn.call(xx)
// this => xx
4. fn.apply(xx)
//this => xx
5. fn.bind(xx)
// this => xx
6. new Fn()
// this => 新的对象
7. fn = ()=> {}
// this => 外面的 this

阐述法

具体的内容参考我写的这篇文章: 《this 的值到底是什么?一次说清楚》