shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.87k stars 504 forks source link

【Q754】实现 LazyMan #810

Open shfshanyue opened 3 months ago

shfshanyue commented 3 months ago
LazyMan('Hank')
// 输出:
// Hi!This is Hank!

LazyMan('Hank').sleep(10).eat('dinner')
// 输出:
// Hi! This is Hank!
// 等待10秒..
// Wake up after 10
// Eat dinner~

LazyMan('Hank').eat('dinner').eat('supper')
// 输出:
// Hi This is Hank!
// Eat dinner~
// Eat supper~

LazyMan('Hank').sleepFirst(5).eat('supper')
// 输出:
// 等待5秒..
// Wake up after 5
// Hi This is Hank!
// Eat supper
HSIKE commented 1 month ago
function LazyMan(name) {
  console.log(`Hi! This is ${name}!`);
}
LazyMan.prototype.sleep = function(time) {
  const timeInMiliSec = time * 1000;
  const start = performance.now();
  console.log(`Wait for ${time} seconds...`);
  while (performance.now() - start <= timeInMiliSec) {}
  console.log(`Wake up after ${time} seconds.`);
  return this;
}
LazyMan.prototype.sleepFirst = function(time) {
  return this.sleep(time);
}
LazyMan.prototype.eat = function(thing) {
  console.log(`Eat ${thing}~~`);
  return this;
}