请解读一下javascript代码,并指出问题所在
var Obj=function(msg){
this.msg=msg;
this.shout=function(){
alert(this.msg);
}
this.waitAndShout=function(){
setTimeout(this.shout, 2000);
}
}
var aa=new Obj("abc");
aa.waitAndShout();
var Obj=function(msg){ this.msg=msg; this.shout=function(){ alert(this.msg);//此this是window,因为调用shout函数的代码在setTimeout函数内部。 } this.waitAndShout=function(){ let t = this.shout;//此this是aa,因为调用waitAndShout函数的对象是aa setTimeout(this.shout, 2000); }}var aa=new Obj("abc");
aa.waitAndShout();
//模拟setTimeout函数的代码
function setTimeout(func,timeSpace){ func();}
p1.work(p1.eat);
