[英]Is it possible to call the function locally defined in another function in JavaScript?
是否可以調用JavaScript中另一個函數中本地定義的函數? 我有一個非常像這樣的代碼:
var module = function () {
function inner() {
// Some code here
}
// Some code here
}
var originalModule = module;
var module = function () {
originalModule();
// inner() must be called here
}
因此,我重寫了原始模塊的實現,但是在新實現中的某個時候,我需要調用inner()函數。 我無法編輯原始實現。 到目前為止,我看到的唯一方法是從原始副本復制inner()函數並在新的函數中定義它。 還有另一種方法嗎?
由於inner()
是在module()
范圍內定義的,因此您不能在此范圍之外訪問它。
此模式用於實現module()
私有方法。
通常這不是一個好習慣,但是您可以執行以下操作 :
function a(x) { // <-- function
function b(y) { // <-- inner function
return x + y; // <-- use variables from outer scope
}
return b; // <-- you can even return a function.
}
a(3)(4); // == 7.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.