【发布时间】:2012-03-23 16:49:24
【问题描述】:
我正在尝试了解 javascript 范围,我想知道是否在以下示例中:
// B can access 'context'
var a = function (context) {
b = function () {
console.log(context);
};
b();
};
a('nicccce'); // works!
// C can't access 'context'
var d = function () {
console.log(context);
};
var c = function (context) {
d();
};
c('oh oh'); // breaks
有没有办法从“d”函数访问“上下文”?
编辑:
我知道我可以将参数传递给 d 函数,这显然是 sane 要做的事情,但我想知道是否有另一种方法,也许使用 this.callee 作为this.callee.arguments[0].
【问题讨论】:
-
您的示例说明了lexical and dynamic scoping 之间的区别。后者在 javascript 中不存在。
-
@thg435 完全理解我的问题:)
标签: javascript node.js