让我们说我有一个由私人和公共方法组成的功能,如下所示:
(function () {
var private_var = "hey";
function private_function () {
// stuff
}
stuff = {
public_var: "hey",
public_function: function () {
// this can be called from the outside with no prob.
},
do_this_now_and_later: (function dothis() {
// i could call this from the namespace "dothis()"
// but not the method name, stuff.do_this_now_and_later()
})()
}
})(window.load = window.load || {});
我想在渲染后立即执行函数do_this_now_and_later()
,但也要稍后再执行。
如果我正确地编写了这个示例代码,那么应该能够通过命名空间dothis()
调用该函数,但是可以通过它的方法调用该函数吗?
答案 0 :(得分:0)
尝试在函数中返回dothis()。
(function () {
var private_var = "hey";
function private_function () {
// stuff
}
stuff = {
public_var: "hey",
public_function: function () {
// this can be called from the outside with no prob.
},
do_this_now_and_later: (function dothis(v) {
alert(v);
return dothis;
// i could call this from the namespace "dothis()"
// but not the method name, stuff.do_this_now_and_later()
})('foo')
}
stuff.do_this_now_and_later('bar');
})(window.load = window.load || {});