this 永远指向函数运行时所在的对象,而不是函数创建时所在的对象
var obj = {
x: 10,
fn: function() {
console.log(this);
console.log(this.x);
}
}
var fn1 = obj.fn;
obj.fn()
// Object {x: 10, fn: function}
// 10
fn1()
// window
//undefined
var obj = {
x: 10;
fn: function () {
function f() {
console.log(this);
console.log(this.x);
}
f();
}
}
var fn = function () {
console.log(this);
console.log(this.x);
}
function Foo() {
console.log(this);
console.log(this.x);
}
obj.fn()
// window
// undefined
fn()
// window
// undefined
Foo()
// window
// undefined