this: 函数执行时才会创建的内部对象,代表持有当前函数的对象.
1.对象内部属性值为函数,当该函数执行时,this表示当前对象
var person = {
name :'xiaoming ',
say:function (){
console.log(this.name) ;
}
}
person.say();
2.构造函数中的this,在执行时表示新对象,即构造函数的实例
function Person(name,age){
this.name= name;
this.age = age;
}
var p1 =new Person('tom',28);
3.call和apply执行函数时.函数this代表call和apply的第一个实参.
function student(proName ){
return this[proName);
}
person.call(window,name);
4.非以上三种场景,this都表示window
function fn(){
console.log(this); //window
}
fn();
function f1(){
console.log(this);
function f2(){
console.log(this);
function f3(){
console.log(this);
}
f3();
}
f2();
}
f1();
网友评论