# ❓ 要求设计 LazyMan 类,实现以下功能

LazyMan('Tony')
// Hi I am Tony

LazyMan('Tony').sleep(10).eat('lunch')
// Hi I am Tony
// 等待了10秒...
// I am eating lunch

LazyMan('Tony').eat('lunch').sleep(10).eat('dinner')
// Hi I am Tony
// I am eating lunch
// 等待了10秒...
// I am eating diner

LazyMan('Tony')
  .eat('lunch')
  .eat('dinner')
  .sleepFirst(5)
  .sleep(10)
  .eat('junk food')
// Hi I am Tony
// 等待了5秒...
// I am eating lunch
// I am eating dinner
// 等待了10秒...
// I am eating junk food
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

直接上代码

class LazyManClass {
  constructor(name) {
    this.name = name
    console.log(`Hi I am ${this.name}`)
    this.tasks = []
    setTimeout(() => this.next())
  }

  sleepFirst(time) {
    var fn = ((t) => () =>
      setTimeout(() => {
        console.log(`等待了${time}秒...`)
        this.next()
      }, t * 1000))(time)

    this.tasks.unshift(fn)
    return this
  }

  sleep(time) {
    var fn = ((t) => () =>
      setTimeout(() => {
        console.log(`等待了${time}秒...`)
        this.next()
      }, t * 1000))(time)

    this.tasks.push(fn)
    return this
  }

  eat(food) {
    var fn = ((f) => () => {
      console.log(`I am eating ${f}`)
      this.next()
    })(food)
    this.tasks.push(fn)
    return this
  }

  next() {
    var fn = this.tasks.shift()
    fn && fn()
  }
}

function LazyMan(name) {
  return new LazyManClass(name)
}

LazyMan('Tony')
LazyMan('Tony').sleep(10).eat('lunch')
LazyMan('Tony').eat('lunch').sleep(10).eat('dinner')
LazyMan('Tony')
  .eat('lunch')
  .eat('dinner')
  .sleepFirst(5)
  .sleep(10)
  .eat('junk food')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58