首页 > 试题广场 >

尝试实现注释部分的Javascript代码,可在其他任何地方

[问答题]
尝试实现注释部分的Javascript代码,可在其他任何地方添加更多代码(如不能实现,说明一下不能实现的原因):
var Obj = function(msg){
	this.msg = msg;
	this.shout = function(){
		alert(this.msg);
	}	
	this.waitAndShout = function(){
		//隔五秒钟后执行上面的shout方法
	}
}
推荐
var Obj = function(msg){
    this.msg = msg;
    this.shout = function(){
        alert(this.msg);
    }    
    this.waitAndShout = function(){
        var that = this;
        setTimeout(that.shout, 5000);
        //隔五秒钟后执行上面的shout方法
    }
    return this;
}
Obj("shouting").waitAndShout();
编辑于 2015-01-06 22:50:18 回复(11)
// 使用bind
// this 指向全局变量 window
var Obj = function(msg){
    this.msg = msg;
    this.shout = function(){
        alert(this.msg);
    }  
    this.waitAndShout = function(){
        setTimeout(function () {
            this.shout();
        }.bind(this), 5000);
    }
}

编辑于 2017-01-04 12:56:04 回复(2)
var Obj = function(msg){
    this.msg = msg;
    this.shout = function(){
        alert(this.msg);
    }  
    this.waitAndShout = function(){
        var that=this;
        setTimeout(function(){
            that.shout();
        },5000);
    }
}
var test=new Obj("a");
test.waitAndShout();

//setTimeout函数作用域

发表于 2015-08-14 13:54:20 回复(0)
this指向错误
应在上方存一下变量
发表于 2014-11-23 13:56:40 回复(0)
为什么我不能运行呢 ?浏览器报错 Cannot read property 'waitAndShout' of undefined
发表于 2017-08-11 11:14:51 回复(0)
题目的本意可能是:
var obj={};
obj.fn = function(msg) {
this.msg = msg;
this.shout = function() {
return this.msg;
}
this.waitAndShout = function() {
}
错误1:
obj.fn = function(msg) {
console.log(this);//this指向obj
this.msg = msg;
this.shout = function() {
console.log(this);
return this.msg;
}
this.waitAndShout = function() {
// 调用 this.shout指向的匿名函数,等价于setTimeout(function(){return this.msg},5000),this指向window而不是obj
setTimeout( this.shout , 5000);
}
}
错误2:
var obj ={};
obj.fn= function(msg) {
console.log(this); //this指向obj
this.msg = msg;
this.shout = function() {
console.log(this);
return this.msg;
}
this.waitAndShout = function() {
var that = this;//保存外部作用域this对象
//调用that.shout指向的匿名函数, 等价于setTimeout(function(){return this.msg},5000), 匿名函数内部的this指向window而不是obj
setTimeout( that.shout , 5000);
}
}
正确:
obj.fn= function(msg) {
console.log(this); //this指向obj
this.msg = msg;
this.shout = function() {
return this.msg;
}
this.waitAndShout = function() {
var that = this;//保存外部作用域this对象
//通过闭包访问外部作用域的that对象的方法,that指向obj
setTimeout(function() {
that.shout();
}, 5000);
}
}
解析:
this指向取决于函数的调用方法,this指向函数的调用者(拥有者)。
  • 方法调用模式

  • 函数调用模式

  • 构造器调用模式

  • apply/call调用模式

匿名函数具有全局性。
每个函数在调用时都会去获取2个值: arguments和this。匿名函数在获取这2个值时,只会搜索自己执行环境,永远不会直接访问外部函数或执行环境。而this指向的是自己的调用者,匿名函数的调用者是window,所以this指向window。

编辑于 2016-09-03 17:36:27 回复(2)
var Obj = function(msg){
            this.msg = msg;
            this.shout = function(){
                alert(this.msg);
            }  
            this.waitAndShout = function(){
                //隔五秒钟后执行上面的shout方法                
                var self=this;
                setTimeout(function(){
                    //console.log(this);//wondow对象
                    //console.log(self);
                    self.shout();        
                },5000);
            }
        };
var obj1=new Obj("hello");
obj1.waitAndShout();
编辑于 2015-04-20 15:43:52 回复(1)
var that = this;
setTimeout(function(){
    that.shout();
})
发表于 2017-08-07 22:54:26 回复(0)
var Obj = function(msg){
    this.msg = msg;
    this.shout = function(){
		console.log(this.msg);
    }   
    this.waitAndShout = function(){
        //隔五秒钟后执行上面的shout方法
		var that=this;
		setTimeout(that.shout,5000);
    }
	return this;
}
var obj=Obj("woshimsg");
obj.waitAndShout();

发表于 2017-07-12 11:13:25 回复(1)
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方法
}
return this;
}
Obj("shouting").waitAndShout();
编辑于 2016-09-20 13:56:21 回复(0)
这道题的考点是:函数内嵌套的函数中,this指向的不是外层函数的上下文,而是指向全局的上下文。所以要先存下外层函数的上下文。
发表于 2016-03-09 20:37:07 回复(1)
定时器中this 指向window
发表于 2015-08-12 16:45:00 回复(0)
var Obj = function(msg){

    this.msg = msg;
    this.shout = function(){
	alert(this.msg);
    }	
    this.waitAndShout = function(){
	setTimeout(this.shout,5000);//隔五秒钟后执行上面的shout方法
    }
}
发表于 2015-03-26 17:32:21 回复(0)
晕头像
window.setInterval(function(){
    this.shout();
},5000)
发表于 2014-12-25 17:15:58 回复(0)
1.代码写的不规范
2.alert执行的内容没有写在一起,
3.调用的函数,没有函数名,并且执行的函数也没有时间设置
发表于 2014-12-22 21:16:48 回复(0)
setTimeout(shout,5000);
发表于 2014-12-16 23:58:35 回复(0)
var self = this;
setTimeout(self.shout);
发表于 2014-12-16 21:43:14 回复(0)
var Obj = function(msg){

this.msg = msg;
this.shout = function(){
alert(this.msg);
}
  this.timeout=function(){
    var that = this;
    setTimeout(function(){
    that.shout()
    
    },2000)
  
  }

}
var aa = new Obj("aa");
aa.timeout();
发表于 2014-12-15 18:55:18 回复(0)
setTimeOut
发表于 2014-12-15 16:05:01 回复(0)
this.waitAndShout = function(){
setTimeout(function(){
     this.shout()
},5000).bind(this);

发表于 2014-12-11 21:56:21 回复(0)
var Obj = function(msg){ this.msg = msg; this.shout = function(){ alert(this.msg); } this.waitAndShout = function(){ setInterval(function(){ Obj.shout(); },5000); } this.waitAndShout(); }
发表于 2014-12-08 12:29:10 回复(0)