简体   繁体   English

在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量?

[英]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.initbar.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.

相关问题 Javascript:如何在do while循环中访问函数范围的变量? - Javascript: How can I access a function-scoped variable within a do while loop? 修改javascript以显示函数范围的变量 - Modify javascript to expose a function-scoped variable 函数范围变量的常量而不是var? - Const instead of var for function-scoped variables? 我该如何解决从函数内部调用函数的元素? - How can I address the element that called a function from within the function? 如何将 key 变量传递给其他地方定义的 eventListener 函数? - How to pass the key variable to eventListener function defined elsewhere? 如何从调用的函数中引用包含事件的元素? - How can I reference the element that contains the event from the function called? Javascript中的变量怎么可以是函数,却不能被调用? - How can a variable in Javascript be a function, but not be able to be called? 如果将被调用方定义为变量,JavaScript函数是否可以知道调用了什么? - Can a JavaScript function know what called it if the callee was defined as a variable? 如何在函数中的javascript中创建2` this`引用 - How to create 2 `this` reference in javascript from within a function 如何从 function 返回之前定义的 useRef 变量? - How can I return a useRef variable, defined before, from a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM