我试图在揭示模式中调用一个私有函数。这是我的代码:
var module = (function(){
var privateMethod = function(val) {
console.log(val);
}
var publicMethod = function() {
var functionString = "privateMethod";
/** This what I tried
functionString.call('test');
window[module.privateMethod]('test');
*/
}
return {
init: publicMethod
}
})();
$(document).ready(function(){
module.init();
});
有人可以帮助我吗?
Thanks!
让你的私有函数成为对象的属性?
var module = (function(){
var privateFuncs = {
privateMethod: function(val) {
console.log(val);
}
};
var publicMethod = function() {
var functionString = "privateMethod";
privateFuncs[functionString]('test');
};
return {
init: publicMethod
};
})();
由于不同的原因,您的其他尝试都失败了:
functionString.call('test')
永远不会工作,因为functionString
指的是字符串文字。它没有一个call
method.
window[module.privateMethod]('test')
不会起作用,因为首先,module
没有财产privateMethod
。如果这样做的话,它就不是“私人的”。这意味着您正在尝试调用window[undefined]
,这不是一个函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)