LazyMan (js)

看了一些大佬的,方法分装的比较好,但是对小白来说看起来比较绕

本菜鸟自己写了容易理解的

!function () {

  var list = []

  function f (e) { }

  f.prototype.eat = function (e) {

    var fn = function () {

      console.log('eat | ' + e)

      调用()

    }

    list.push(fn)

    return this

  }

  f.prototype.sleep = function (e) {

    var fn = function () {

      setTimeout(() => {

        console.log('sleep | ' + e)

        调用()

      }, e * 1000)

    }

    list.push(fn)

    return this

  }

  f.prototype.firstSleep = function (e) {

    var fn = function () {

      setTimeout(() => {

        console.log('firstSleep | ' + e)

        调用()

      }, e * 1000)

    }

    list.unshift(fn)

    return this

  }

  function 调用 () { list.length > 0 && list.shift()() }

  window.f = function (e) {

    var fn = function () {

      console.log('lazyman | ' + e)

      调用()

    }

    list.push(fn)

    setTimeout(调用, 0)

    return new f()

  }

}()

// 下面4行是调用的

f("Hank").sleep(2).eat("dinner")

f("Hank").eat("dinner").eat("supper")

f("Hank").firstSleep(2).eat("supper")

f("Hank").sleep(2).firstSleep(3).eat("supper")

你可能感兴趣的:(LazyMan (js))