[英]In Javascript, how can I reference a function-scoped variable from a function called within that function but defined elsewhere?
I have a Javascript function something like this, defined in a place I can't change it: 我有一个类似这样的Javascript函数,它在无法更改的地方定义:
foo.customSave = [];
foo.save = function() {
var saveString = 'initial value';
// stuff about saveString here
for (i in foo.customSave) { foo.customSave[i](); }
// more stuff about saveString here, including actually saving it
};
Then I have my own code like this: 然后我有自己的代码,如下所示:
bar.init = function() {
// other init stuff here
foo.customSave.push(function() {
saveString += 'something meaningful here';
});
// more other init stuff here
};
bar.init()
is called at an appropriate time (which is to say, before foo.save()
is called). bar.init()
被调用在适当的时间(这就是说,前foo.save()
被调用)。 The problem seems to be that saveString
is not defined when I try to add 'something meaningful here'
to it (putting a console.log
call there confirms this). 问题似乎是当我尝试向其中添加
'something meaningful here'
时未定义saveString
(将console.log
调用放在'something meaningful here'
即可确认这一点)。
Is there any way my customSave
function can access that string, or am I stuck? 我的
customSave
函数可以以任何方式访问该字符串,还是卡住了?
Given that you can't modify the function associated with foo.save
, there is no way you can modify the saveString
variable. 鉴于您无法修改与
foo.save
关联的函数,因此无法修改saveString
变量。 The reason for this is because saveString
is a local variable that is scoped to function associated with foo.save
. 这是因为
saveString
是局部变量,其作用域是与foo.save
关联的foo.save
。 As a result, you can call the function, but it basically acts as a black box where you can't access the variables defined it in (in fact, without looking at the source code, you wouldn't even know that the saveString
variable existed). 结果,您可以调用该函数,但是它基本上就像一个黑匣子,在其中您无法访问在其中定义的变量(实际上,如果不查看源代码,您甚至不会知道
saveString
变量存在)。
Within the function associated with bar.init
, you're creating a new function object each time bar.init
is called and pushing it to an array. 在与相关的功能
bar.init
,你要创建一个新的函数对象每次bar.init
被调用,它推到一个数组。 And because you haven't used var
to declare the saveString
variable, JavaScript will try to find the saveString
variable within the function being pushed to an array. 并且因为您没有使用
var
来声明saveString
变量,所以JavaScript会尝试在要推送到数组的函数中找到saveString
变量。 Since it can't find the declaration there, JavaScript will continue looking up the variable in the next outer scope which the the function associated with bar.init
. 由于在此处找不到声明,因此JavaScript将继续在下一个外部范围中
bar.init
与bar.init
关联的函数中的bar.init
。 Since it can't find it there either, JavaScript will lastly try to find it in the global scope (and won't have success there either). 由于也无法在其中找到JavaScript,因此JavaScript会最后尝试在全局范围内找到它(也不会在其中获得成功)。
Hope that helps, but long story short, without being able to modify foo.save
, you're stuck. 希望能有所帮助,但总而言之,不能修改
foo.save
,您会陷入困境。
How about adding saveString
as a property of foo
? 如何将
saveString
添加为foo
的属性?
Like so: 像这样:
foo.customSave = [];
foo.saveString = '';
foo.save = function() {
foo.saveString = 'initial value';
// stuff about saveString here
for (i in foo.customSave) { foo.customSave[i](); }
// more stuff about saveString here, including actually saving it
};
Then: 然后:
bar.init = function() {
// other init stuff here
foo.customSave.push(function() {
foo.saveString += 'something meaningful here';
});
// more other init stuff here
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.