Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

惰性函数 #12

Open
xiaohesong opened this issue Nov 26, 2018 · 7 comments
Open

惰性函数 #12

xiaohesong opened this issue Nov 26, 2018 · 7 comments
Labels
function tips 一些不错的想法

Comments

@xiaohesong
Copy link
Owner

今天看到一篇文章中有提到 惰性函数 。感觉很新奇,去查了下,发现就是自己调用自己。

可以让你只在第一次的时候调用你需要处理的函数,后面可以直接使用函数。

var foo = function() {
  console.log('你会发现我只出现一次哦,不管你调用几次')
  foo = function() {
    console.log('嘻嘻,我出现了哦')
  }
  foo()
}

foo() // 你会发现我只出现一次哦, 不管你调用几次; 嘻嘻,我出现了哦
foo() //嘻嘻,我出现了哦

还挺有意思的,留下个问题,你觉得这个是因为啥,为啥会出现上面的情况?

@xiaohesong xiaohesong added tips 一些不错的想法 function labels Dec 1, 2018
@YuYuBei
Copy link

YuYuBei commented Dec 7, 2018

因为上面第三行的时候,把foo函数重写了,所以未来只会有 “嘻嘻,我出现了哦”

@medeen
Copy link

medeen commented Dec 7, 2018

试了下,这样就不行。

var foo = function() {
  console.log('你会发现我只出现一次哦,不管你调用几次')
  var foo = function() {
    console.log('嘻嘻,我出现了哦')
  }
  foo()
}
foo()
foo()

😢

@YuYuBei
Copy link

YuYuBei commented Dec 7, 2018

试了下,这样就不行。

var foo = function() {
  console.log('你会发现我只出现一次哦,不管你调用几次')
  var foo = function() {
    console.log('嘻嘻,我出现了哦')
  }
  foo()
}
foo()
foo()

😢

这是因为你在第三行的时候,重新var了一下foo(新foo),这个新foo的作用域是在范围比较大的foo(旧foo)里面,所以不能把旧foo进行重新赋值为一个新foo,这样每次运行的时候,都是存在两个不同的foo

@xiaohesong
Copy link
Owner Author

@YuYuBei 👍
@medeen 可以看下这个文章js的执行上下文 对js的执行有个了解

@YuYuBei
Copy link

YuYuBei commented Dec 10, 2018

@xiaohesong 三克油,这些都是你自己翻译的吗?

@xiaohesong
Copy link
Owner Author

@YuYuBei 嗯,看到一些不错的会记录下来

@xiaohesong
Copy link
Owner Author

class Man {
    constructor(name) {
        this.name = name
        console.log(`Hi! This is ${name}`)
        this.queue = []
        setTimeout(() => {
            this.next()
        }, 0)
    }

    toQueue(fn, isFirst) {
        if(isFirst) {
            this.queue.unshift(fn)
        } else {
            this.queue.push(fn)
        }
        return this
    }

    next() {
        const fn = this.queue.shift()
        fn && fn()
    }

    eat(food) {
        const eater = () => {
            console.log(`Eat ${food}`)
            this.next()
        }
        this.toQueue(eater)
        return this
    }

    sleepFirst(time) {
        this.sleep(time, true)
    }

    sleep(time, isFirst) {
        const sleeper = () => {
            setTimeout(() => {
                console.log(`Wake up after ${time}`)
                this.next()
            }, time * 1000)
        }
        this.toQueue(sleeper, isFirst)
        return this
    }

}

const LazyMan = (name) => new Man(name)

LazyMan('小明').eat('午餐').sleep(2).eat('晚餐').sleepFirst(3);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function tips 一些不错的想法
Projects
None yet
Development

No branches or pull requests

3 participants