腾讯笔试遇到的一道题,百思不得其解,求助大牛们解答
实现一个 HardMan:HardMan("jack") 输出:
I am jack
HardMan("jack").rest(10).learn("computer") 输出
I am jack
//等待10秒
Start learning after 10 seconds
Learning computer
HardMan("jack").restFirst(5).learn("chinese") 输出
//等待5秒
Start learning after 5 seconds
I am jack
Learning chinese
我的答案:
function HardMan(name){
var fucntionTarget = '';
this.rest = function(minnus){
var timer = setInterval(function(){
console.log("Start learning after "+ minnus +" seconds");
console.log('Learning '+fucntionTarget);
clearInterval(timer);
},minnus*1000);
return this;
};
this.learn = function(target)
{
fucntionTarget = target;
};
this.restFirst = function(minnus){
};
console.log('I am '+ name);
return this;
}
#腾讯#var fucntionTarget = '';
this.rest = function(minnus){
var timer = setInterval(function(){
console.log("Start learning after "+ minnus +" seconds");
console.log('Learning '+fucntionTarget);
clearInterval(timer);
},minnus*1000);
return this;
};
this.learn = function(target)
{
fucntionTarget = target;
};
this.restFirst = function(minnus){
这个不会写。
};
console.log('I am '+ name);
return this;
}