我有一个对象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'的范围 - 想法?
JavaScript范围不起作用,因为它具有(仅)函数范围(使用let关键字时除外).
你可以做的是...
obj = {
a: 0,
funcA: function() {
this.a = 'a';
},
funcB: function() {
console.log(this.a);
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |