一、函数预编译过程this—>window
function test(x){
var a=123;
function b(){}
}
test(1)
预编译过程中
test()
AO{
arguments[1]
this: window
a:undefined
b:function(){}
x:1
}
构造函数
function Test(){
//new 之后,将原来AO中this替换掉
var this=Object .create(Test.prototype)
var this={
__proto__:Test.prototype
}
}
var test=new Test()
二、在全局作用域里this——>window
三、call\apply改变this指向
四、obj.func() func()里面的this指向obj
谁调用这个方法,这个方法里面的this指向谁
var obj={
a:function(){
console.log(this.name)
}
name:"xiaoming"
}
obj.a()
//a()里面的this指向obj
练习
var name="222";
var a={
name:"111",
say:function(){
console.log(this.name)
}
};
var fun = a.say;
fun();//222 //a.say函数引用放到了fun变量,fun在全全局里执行
a.say();//111
var b={
//第三个函数执行
//this-->b
name:"333",
say:function (fun) {
fun()//实参a.say-->function(){console.log(this.name)}
//但func()执行并不是b对象调用,走预编译环节,this->window
}
};
b.say(a.say);//222
b.say=a.say;
b.say()//333