Accessing variables trapped by closure

前端 未结 6 609
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 06:38

I was wondering if there is any way to access variables trapped by closure in a function from outside the function; e.g. if I have:


A = function(b) {
    va         


        
6条回答
  •  感情败类
    2020-11-29 06:59

    The whole point to that pattern is to prevent 'c' from being accessed externally. But you can access foo() as a method, so make it that it will see 'c' in its scope:

    A = function(b) {
        var c = function() {//some code using b};
        this.foo = function() {
            return c();
        }
    }
    

提交回复
热议问题