this
- this指向问题,一般情况下this的最终指向的是那个调用的对象。
this
// Window {window: Window, self: Window, document: document, name: "", location: Location, …}
- 全局作用域或者普通函数中this指向全局对象window(注意定时器里面的this指向window)。
function fn(){console.log(this);}fn()
// VM825:1 Window {window: Window, self: Window, document: document, name: "", location: Location, …}
window.fn();
window.setTimeout(function(){console.log(this);})
// 31329
// VM1097:1 Window {window: Window, self: Window, document: document, name: "", location: Location, …}
- 构造函数中this指向构造函数的实例
var o={sayHi:function(){console.log(this);}};o.sayHi();
// VM1239:1 {sayHi: ƒ}