从jQuery命名空间
问题描述:
这里的外部调用jQuery的命名空间的功能是代码从jQuery命名空间
test ('e');
function test(e) {
test2(e); //undefined
}
(function ($) {
function test2(e) {
alert('test');
}
})
由于一些限制,我有这样的呼吁。有谁知道?
答
可以,通过声明test2
匿名函数外:
var test2; //Declare test2 in global
(function ($) {
test2 = function (e) { //define test2
alert('test'); //because test2 was declared in global,
}; // it will stay global.
})(jQuery);
test('e'); //Call test
function test(e) {
test2(e); //Since test2 can be access anywhere in your code,
} //it is now defined.
答
您不能,函数test2
已经在闭包中定义,您只能调用该范围内的函数。在文件
答
绑定事件,触发它:
function test(e) {
var param=1;
param2=4;
jQuery(document).trigger('mytest2',[param,param2]);
}
(function ($) {
$(document).bind('mytest2',test2);
function test2(event,param,param2) {
alert('test '+param+' '+param2);
}
})(jQuery)
setTimeout(test,2000);
最好回答,谢谢 – hkguile 2013-03-13 03:36:34
@hkinterview - 不客气。 PS:hkinterview,香港慨? – 2013-03-13 03:47:44
系呀,你是香港人? ............. – hkguile 2013-03-13 03:52:54