Created
February 25, 2017 14:49
-
-
Save cchang62/b8c3e975f4463718570cbd2ab3d71b83 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kuruxac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function _LazyMan(name) { | |
this.tasks = []; | |
var self = this; | |
var fn =(function(n){ | |
var name = n; | |
return function(){ | |
console.log("Hi! This is " + name + "!"); | |
self.next(); | |
} | |
})(name); | |
this.tasks.push(fn); | |
setTimeout(function(){ | |
self.next(); | |
}, 0); // 在下一個事件循環啟動任務 | |
} | |
/* 事件調度函數 */ | |
_LazyMan.prototype.next = function() { | |
var fn = this.tasks.shift(); | |
fn && fn(); | |
} | |
_LazyMan.prototype.eat = function(name) { | |
var self = this; | |
var fn =(function(name){ | |
return function(){ | |
console.log("Eat " + name + "~"); | |
self.next() | |
} | |
})(name); | |
this.tasks.push(fn); | |
return this; // 實現鍊式調用 | |
} | |
_LazyMan.prototype.sleep = function(time) { | |
var self = this; | |
var fn = (function(time){ | |
return function() { | |
setTimeout(function(){ | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.push(fn); | |
return this; | |
} | |
_LazyMan.prototype.sleepFirst = function(time) { | |
var self = this; | |
var fn = (function(time) { | |
return function() { | |
setTimeout(function() { | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.unshift(fn); | |
return this; | |
} | |
/* 封裝 */ | |
function LazyMan(name){ | |
return new _LazyMan(name); | |
} | |
LazyMan("Hank").sleepFirst(3).eat("dinner"); | |
LazyMan("Jim").sleep(5).eat("lunch"); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function _LazyMan(name) { | |
this.tasks = []; | |
var self = this; | |
var fn =(function(n){ | |
var name = n; | |
return function(){ | |
console.log("Hi! This is " + name + "!"); | |
self.next(); | |
} | |
})(name); | |
this.tasks.push(fn); | |
setTimeout(function(){ | |
self.next(); | |
}, 0); // 在下一個事件循環啟動任務 | |
} | |
/* 事件調度函數 */ | |
_LazyMan.prototype.next = function() { | |
var fn = this.tasks.shift(); | |
fn && fn(); | |
} | |
_LazyMan.prototype.eat = function(name) { | |
var self = this; | |
var fn =(function(name){ | |
return function(){ | |
console.log("Eat " + name + "~"); | |
self.next() | |
} | |
})(name); | |
this.tasks.push(fn); | |
return this; // 實現鍊式調用 | |
} | |
_LazyMan.prototype.sleep = function(time) { | |
var self = this; | |
var fn = (function(time){ | |
return function() { | |
setTimeout(function(){ | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.push(fn); | |
return this; | |
} | |
_LazyMan.prototype.sleepFirst = function(time) { | |
var self = this; | |
var fn = (function(time) { | |
return function() { | |
setTimeout(function() { | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.unshift(fn); | |
return this; | |
} | |
/* 封裝 */ | |
function LazyMan(name){ | |
return new _LazyMan(name); | |
} | |
LazyMan("Hank").sleepFirst(3).eat("dinner"); | |
LazyMan("Jim").sleep(5).eat("lunch");</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _LazyMan(name) { | |
this.tasks = []; | |
var self = this; | |
var fn =(function(n){ | |
var name = n; | |
return function(){ | |
console.log("Hi! This is " + name + "!"); | |
self.next(); | |
} | |
})(name); | |
this.tasks.push(fn); | |
setTimeout(function(){ | |
self.next(); | |
}, 0); // 在下一個事件循環啟動任務 | |
} | |
/* 事件調度函數 */ | |
_LazyMan.prototype.next = function() { | |
var fn = this.tasks.shift(); | |
fn && fn(); | |
} | |
_LazyMan.prototype.eat = function(name) { | |
var self = this; | |
var fn =(function(name){ | |
return function(){ | |
console.log("Eat " + name + "~"); | |
self.next() | |
} | |
})(name); | |
this.tasks.push(fn); | |
return this; // 實現鍊式調用 | |
} | |
_LazyMan.prototype.sleep = function(time) { | |
var self = this; | |
var fn = (function(time){ | |
return function() { | |
setTimeout(function(){ | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.push(fn); | |
return this; | |
} | |
_LazyMan.prototype.sleepFirst = function(time) { | |
var self = this; | |
var fn = (function(time) { | |
return function() { | |
setTimeout(function() { | |
console.log("Wake up after " + time + "s!"); | |
self.next(); | |
}, time * 1000); | |
} | |
})(time); | |
this.tasks.unshift(fn); | |
return this; | |
} | |
/* 封裝 */ | |
function LazyMan(name){ | |
return new _LazyMan(name); | |
} | |
LazyMan("Hank").sleepFirst(3).eat("dinner"); | |
LazyMan("Jim").sleep(5).eat("lunch"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment