class Hero {
constructor (name) {
this.name = name
console.log(`hi this is ${this.name}`)
}
kill (num) {
let complex = (num === 1) ? 'bug' : 'bugs'
console.log(`kill ${num} ${complex}`)
return this
}
recover (num) {
let complex = (num === 1) ? 'blood' : 'bloods'
console.log(`recover ${num} ${complex}`)
return this
}
sleep (time) {
let now = +new Date()
while (+new Date() - now < time * 1000) {}
return this
}
}
new Hero('aaa').sleep(10).kill(2).recover(1)
不想写原型啊什么的,用的ES6
不知道是不是这么个意思, JS单线程还想玩sleep,那我只能循环了。。
希望有大佬能给出好答案吧