[英]dynamically call local function in javascript
關於按名稱動態調用函數有很多類似的問題。 但是,我無法找到解決我的具體問題的方法,其中我在閉包內部有本地函數,而不將函數暴露給我的對象的公共接口。
讓我們看一些代碼(這是一個虛構的例子)......
(function(window,$) {
MyObject = (function($) {
var obj = {};
obj.publicMethod = function(number,otherarg) {
this['privateMethod'+number].apply(this,[otherarg]);
};
var privateMethod1 = function(arg) {
//do something with arg
};
var privateMethod2 = function(arg) {
//do something else with arg
};
return obj;
})($);
window.MyObject = MyObject;
})(window,jQuery);
這不起作用,因為“this”是MyObject,並且不公開本地函數。 此外,我希望能夠在嘗試調用之前檢查函數是否存在。 例如。
var func_name = 'privateMethod'+number;
if($.isFunction(this[func_name])) {
this[func_name].apply(this,[otherarg]);
}
我不確定如何繼續進行,除了將我的私有函數暴露給公共接口之外,它一切正常。
obj.privateMethod1 = function(arg) {
//do something with arg
};
obj.privateMethod2 = function(arg) {
//do something else with arg
};
我的想法已經不多了。 非常感謝您的幫助和建議。
私有函數是局部變量,不是任何對象的一部分。 因此, [...]
記法訪問屬性是從來沒有去上班,因為沒有對象的私有函數的性質。
相反,你可以制作兩個對象: private
和public
:
var public = {},
private = {};
public.publicMethod = function(number, otherarg) {
// `.apply` with a fixed array can be replaced with `.call`
private['privateMethod' + number].call(this, otherarg);
};
private.privateMethod1 = function(arg) {
//do something with arg
};
private.privateMethod2 = function(arg) {
//do something else with arg
};
return public; // expose public, but not private
您無法通過字符串獲取對局部變量的引用。 您必須將本地對象添加到命名空間:
(function(window,$) {
// Use "var MyObject = " instead of "MyObject = "!! Otherwise, you're assigning
// the object to the closest parent declaration of MyVar, instead of locally!
var MyObject = (function($) {
var obj = {};
var local = {}; // <-- Local namespace
obj.publicMethod = function(number,otherarg) {
local['privateMethod'+number].call(this, otherarg);
};
var privateMethod1 = local.privateMethod1 = function(arg) {
//do something with arg
};
var privateMethod2 = local.privateMethod2 = function(arg) {
//do something else with arg
};
return obj;
})($);
window.MyObject = MyObject;
})(window,jQuery);
我很驚訝不正確的答案被標記為已被接受。 實際上,您可以通過字符串獲取對局部變量的引用。 只需使用eval
:
(function(window,$) {
MyObject = (function($) {
var obj = {};
obj.publicMethod = function(number,otherarg) {
// Gets reference to a local variable
var method = eval('privateMethod'+number);
// Do with it whatever you want
method.apply(this,[otherarg]);
};
var privateMethod1 = function(arg) {
//do something with arg
};
var privateMethod2 = function(arg) {
//do something else with arg
};
return obj;
})($);
window.MyObject = MyObject;
})(window,jQuery);
實際上這個代碼非常糟糕,99.9%的情況下你不應該使用eval
。 但是你必須知道它是如何工作的以及你可以用它做什么。 當我需要使用eval
時,我自己有一些非常具體的案例。
假設您只有幾個要調用的函數,您可以創建自己的Window版本來調用函數:
var myFuncs = {
'foo': foo,
'bar': bar
};
然后在你的代碼中:
var s = 'foo';
myFuncs[s]();
只需確保在將對象添加到對象時定義了這些函數。 在加載時不存在函數的模塊中,可以在初始化模塊時添加它們:
var myFuncs = {};
var init = function(){
myFuncs['foo'] = foo;
myFuncs['bar'] = bar;
}
您不能從定義它們的范圍之外調用這些函數的事實是javascript的基本部分,實際上是所有編程語言。
調用這些函數的唯一方法是將它們公開。 然而,可以應用基於約定的方法。 下划線前綴相當普遍,通常被理解為“不打算被稱為公共函數”,例如:
obj._privateMethod1 = function(arg) {
//...
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.