【发布时间】:2013-05-05 15:48:26
【问题描述】:
好的,给定以下我不想修改的Javascript 代码:
(function () {
function iWantToCallThis(){
// Do some stuff
}
window.SomeObject = {
theirfunc = function(){
// Do some stuff
},
otherFuncIDontWantToCall = function(){
// This works, but don't want to call this function.
iWantToCallThis();
// does other stuff
}
}
}());
我怎样才能像这样通过 SomeObject 的范围访问iWantToCallThis():
window.SomeObject.theirfunc = (function (func){
iWantToCallThis();
func.apply(this, arguments);
} (win.SomeObject.theirfunc));
尽管我认为该函数在技术上可以在其原始范围内运行,但我无权访问iWantToCallThis()。有什么方法可以在不编辑原始源代码的情况下访问该功能?
谢谢!
【问题讨论】:
-
你不能。
iWantToCallThis是您的自执行匿名函数的本地。这不是属性。 -
恕我直言,Douglas Crockford 的“Private Members in JavaScript”是关于该主题的权威文章。它应该有助于说服您 Private 成员在外部无法访问,除非通过 Privileged 方法。
-
很棒的文章@Beetroot-Beetroot,介意将其发布为答案,我会接受吗?绝对可以更好地深入解释我遇到的情况。
标签: javascript closures scope