this是js的一个关键字,随着函数使用场合不同,this的值会发生变化。但是总有一个原则,那就是this指的是调用函数的那个对象。
1、纯粹函数调用。
function test() { this.x = 1; alert(x); } test();
其实这里的this就是全局变量。看下面的例子就能很好的理解其实this就是全局对象Global。
var x = 1; function test() { alert(this.x); } test();//1 var x = 1; function test() { this.x = 0; } test(); alert(x);//0
2、作为方法调用,那么this就是指这个上级对象。
function test() { alert(this.x); } var o = {}; o.x = 1; o.m = test; o.m(); //1
3、作为构造函数调用。所谓构造函数,就是生成一个新的对象。这时,这个this就是指这个对象。
function test() { this.x = 1; } var o = new test(); alert(o.x);//1
4、apply调用
this指向的是apply中的第一个参数。
var x = 0; function test() { alert(this.x); } var o = {}; o.x = 1; o.m = test; o.m.apply(); //0 o.m.apply(o);//1
当apply没有参数时,表示为全局对象。所以值为0。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
//不用构造函数 Obj中的this 指向的是window
/* var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(this.shout(),5000);//隔五秒钟后执行上面的shout方法
}
};
Obj("shouting").shout();
//为了指向Obj 想到了用构造函数*/
//一、人人都会想到的写法:但不会延时
var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(this.shout(),5000);//隔五秒钟后执行上面的shout方法
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window
/*
//二、为了延时,在setTimeout里写上函数function(){}的形式
//报错this.shout is not a function 说明setTimeout中的this 没有指向Obj 接下来看看this指向了哪里
/*var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function(){this.shout()},5000);//隔五秒钟后执行上面的shout方法 报错this.shout is not a function
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window*/
// 三alert一下,说明函数的函数this指向window
/*
var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function(){alert(this)},5000);//隔五秒钟后执行上面的shout方法
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window
*/
//四 为了让this 指向 Obj 定义一个局部变量,
// 由于 构造函数定以后,this 指向Obj;所以局部变量that也指向Obj
// 成功
/*var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
var that=this;
setTimeout(function(){that.shout()},5000);//隔五秒钟后执行上面的shout方法
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window*/
//考虑在setTimeout函数内部将 指向Obj的this传进去,然后 将self=this
/* var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function () {
var self = this;
return function () {
self.shout();
}
}.call(this), 5000);
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window*/
//如果不用self = this;仅仅是将指向Obj的this 传进去;函数的函数 用this this还是指向window;
// 只有 函数1 的 函数2 之间的this才指向函数1内this该指的对象;由于call已经将this-》Obj传入
//所以函数1 内this 指向就是Obj
/* var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function () {
return function () {
alert(this);
}
}.call(this), 5000);
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window*/
//不要函数的函数;this 就是call传进来的this 这样 alert就是window 说明我想的是正确的
/* var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function () {
alert(this);
}.call(this), 5000);
}
};
var a=new Obj("shouting");//this指向Obj 函数里的this 都指向Obj
a.waitAndShout();//this指向window*/
//不用构造函数 此法不太好 污染window对象;但是可以实现,关键是return this;
//加不加var that=this 结果都是一样的; 因为this一直指向的是window ;
// this 和that 都指向window
//而 return this 是吧this 返回到window中
//所以that 其实也指的window 只不过,Obj的变量都变成全局的了
/* var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
};
this.waitAndShout = function(){
setTimeout(function(){this.shout()}, 5000);
//隔五秒钟后执行上面的shout方法
};
return this;
};
Obj("shouting").waitAndShout();*/
</script>
</body>
</html>