首页 > 代码库 > 实现一个简单的lazyman
实现一个简单的lazyman
function lazyman(name) { return new lazyman.fn.init(name); } lazyman.fn = lazyman.prototype = { construct: lazyman, stack: null, status: 0, init: function (name) { this.name = name; this.stack = []; return this; }, sleep: function (time) { var that = this; time = +time; if(time !== time) { this.next(); } else if(this.status) { this.stack.unshift(this.sleep.bind(this, time)); } else { this.status = 1; this.print(`sleeping ${time} seconds...`); setTimeout(function () { that.status = 0; that.next(); }, time * 1000); } return this; }, next: function () { while(this.status == 0 && this.stack.length) { this.print( this.stack.pop()() ); } return this; }, eat: function(thing) { this.stack.unshift(lazyEat.bind(null, this.name, thing)); if(this.status == 0) { this.next(); } return this; }, print: console.log } lazyman.fn.init.prototype = lazyman.fn; function lazyEat(name, thing) { return `${name} eat ${thing}`; } module.exports = lazyman;
实现一个简单的lazyman
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。