腾讯笔试遇到的一道题,百思不得其解,求助大牛们解答

实现一个 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;
}
#腾讯#
全部评论
function hardMan(str) { this.queue = [] this.name = str this.queue.push(() => { console.log(this.name); this.next() }); } hardMan.prototype.rest = function (wait) { const func = () => { setTimeout(() => { console.log(`Start learning after ${wait} seconds`) this.next() }, wait * 1000) } this.queue.push(func) return this } hardMan.prototype.restFirst = function (wait) { const func = () => { setTimeout(() => { console.log(`Start learning after ${wait} seconds`) this.next() }, wait * 1000) } this.queue.unshift(func) return this } hardMan.prototype.learn = function (str) { const func = () => { console.log(`Learning ${str}`) } this.queue.push(func) this.next() } hardMan.prototype.next = function () { if (this.queue.length === 0) return const func = this.queue.shift() func() } const HardMan = (name)=>{ return new hardMan("jack"); }
4 回复 分享
发布于 2023-08-09 17:56 北京
function Hard(str){     var that = {};     that.__proto__ = arguments.callee.prototype ;     that.str = "I am " + str;     setTimeout(that.settime.bind(that));     return that; } Hard.prototype.yanchi = function(m){     console.log("sleep")     var d = new Date();     var n = this._restFirst*1000;     for(var i = 0; i < d; i++){         if( new Date() - d > n ){             console.log("sleep end")             return false;         }     } } Hard.prototype.learn = function( sr ) {     this._learn = sr;     return this; } Hard.prototype.rest = function( num ){     this._rest = num;     return this; } Hard.prototype.restFirst = function ( n ) {     this._restFirst = n;     this._rest = 0;     return this; } Hard.prototype.settime = function(){     var { str, _learn, _rest, _restFirst} = this;     var num = _rest || _restFirst;     if( _restFirst !== undefined ){         this.yanchi();     }else{         console.log(str);     }     if(num){         setTimeout(function(){             console.log( `Start learning after ${ num } seconds` );             if( _restFirst !== undefined ){                 console.log(str);             }             if( _learn ){                 console.log( "Learning " + _learn )             }         },_rest*1000)     } }
1 回复 分享
发布于 2018-04-18 23:28
class HardMan {constructor(name) {this.name = name;this.taskQueue = Promise.resolve();//考察执行栈和事件循环 setTimeout(() => {this.execute();})}execute() {if (this._restFirst) {this.realRest(this._restFirst);this._restFirst = 0;}this.outName();if (this._rest) {this.realRest(this._rest)}if (this._learn) {this.taskQueue = this.taskQueue.then(() => {console.log(`Learning ${this._learn}`)});}}outName() {this.taskQueue = this.taskQueue.then(() => {console.log(`I am ${this.name}`)});return this;}rest(seconds) {this._rest = seconds;return this;}realRest(seconds) {this.taskQueue = this.taskQueue.then(() => { return new Promise(resolve => {setTimeout(() => {console.log(`After rest ${seconds} seconds`);resolve();}, seconds * 10);});});return this;}restFirst(seconds) {this._restFirst = seconds;return this;}learn(subject) {this._learn = subject;return this;}}
1 回复 分享
发布于 04-17 22:35 湖南
哪个事业群的面试题
点赞 回复 分享
发布于 2018-04-17 10:43
微信小程序事业群的
点赞 回复 分享
发布于 2018-04-17 23:03
跟lazyman一摸一样的呀
点赞 回复 分享
发布于 2018-04-18 23:31
去年腾讯面试遇到了这道题,当时没写出来,后来想了想写出来了,给大家两个种满分解题代码吧:https://mp.weixin.qq.com/s/-hEjxY0yI6ZBTzAlc3xwAw
点赞 回复 分享
发布于 08-23 09:50 广东

相关推荐

3 13 评论
分享
牛客网
牛客企业服务