函数预编译过程this-window
function test(c) { var a = 123; function b() {} console.log(this); } test(); //AO{ // arguments:[1], // this:window, // C:1, // a:undefined, // b:function(){} // }
全局作用域this->window
call/applay改变this指向
function Person(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } function Xiaoming(name,age,sex,weight){ Person.call(this,name,age,sex); this.weight=weight; } var xiaoming = new Xiaoming("ming",1,2,180);
查看结果如下图
obj.func() func()里面的this指向obj,谁调用指向谁
var obj = { a: function () { console.log(this.name) }, name: "abc" } obj.a();