将范围从一个函数传递到另一个函数

qui*_*ton 0 javascript scope

我有一个对象litrel,有2个匿名函数定义如下:

obj = {
    funcA : function(){
        var a = 'a';
    },
    funcB : function(){
        // but how do you access the scope in 'funcA' to access variable 'a'?
        console.log(a)
    }
}
Run Code Online (Sandbox Code Playgroud)

我不想传递任何变量只是'funcA'的范围 - 想法?

ale*_*lex 6

JavaScript范围不起作用,因为它具有(仅)函数范围(使用let关键字时除外).

可以做的是...

obj = {
    a: 0,
    funcA: function() {
        this.a = 'a';
    },
    funcB: function() {
        console.log(this.a);
    }
};
Run Code Online (Sandbox Code Playgroud)

jsFiddle.