美文网首页
LazyMan (js)

LazyMan (js)

作者: jiangji | 来源:发表于2018-11-28 11:22 被阅读0次

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

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

    !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)

          本文链接:https://www.haomeiwen.com/subject/zzraqqtx.html