<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Bin</title>
</head>
<body>
<script type="text/javascript">
function LazyMan(name) {
var api = {
sleep: function (second) {
task.push(function () {
setTimeout(function () {
console.log("Wake up after " + second)
next()
},second*1000)
})
return this
},
sleepFirst: function (second) {
task.unshift(function () {
setTimeout(function () {
console.log("Wake up after " + second)
next()
},second*1000)
})
return this
},
eat: function (what) {
task.push(function () {
console.log("Eat " + what)
next()
})
return this
},
say: function (name) {
task.push(function () {
console.log("Hi, this is " + name)
next()
})
return this
}
}
var task = []
var next = function () {
var fn = task.shift()
fn && fn.call()
}
api.say(name)
setTimeout(function () {
next()
},0)
return api
}
LazyMan("Hank").sleepFirst(3).eat("dinner")
</script>
<script type="text/javascript">
var _loop = function _loop(i) {
setTimeout(function () {
console.log(i);
},100*i)
}
for (var i = 0; i < 10; i++) {
_loop(i)
}
for (var j = 0; j < 10; j++) {
setTimeout(function () {
console.log(j);
},100*j)
}
var myTeacher = "Mrs. Tysick"
var yourTeacher = myTeacher
console.log(myTeacher)
console.log(yourTeacher)
myTeacher = "Mrs. Goodyear"
console.log(myTeacher)
console.log(yourTeacher)
</script>
</body>
</html>