类似JQ的链式调用
;(function(){
window.Hero = function(name, num, bloods){
console.log(this.name);
return new _Hero(name, num, bloods);
};
function _Hero(name, num, bloods) {
this.name = name;
this.num = num;
this.bloods = bloods;
return this;
};
_Hero.prototype = {
constructor : _Hero,
kill: function () {
if (this.num === 1) {
console.log("kill " + this.num + " bug");
}
else {
console.log("kill " + this.num + " bugs");
}
return this;
},
recover: function () {
if (this.bloods === 1) {
console.log("recover " + this.bloods + " blood");
}
else {
console.log("recover " + this.bloods + " bloods");
}
return this;
},
sleep: function () {
setTimeout((function () {
// 等待
console.log(' ');
})(), 10000);
return this;
}
};
})();
Hero("aaa").sleep(10).kill(2);