これがコードです
test ('e');
function test(e) {
test2(e); //undefined
}
(function ($) {
function test2(e) {
alert('test');
}
})
何かの制限があるので、私はこのように呼ばなければなりません。誰もが知っていますか?
これがコードです
test ('e');
function test(e) {
test2(e); //undefined
}
(function ($) {
function test2(e) {
alert('test');
}
})
何かの制限があるので、私はこのように呼ばなければなりません。誰もが知っていますか?
できません。関数test2
はクロージャーで定義されています。そのスコープ内でのみ関数を呼び出すことができます。
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.
デモ: http: //jsfiddle.net/DerekL/LEZkt/
ドキュメントのイベントをバインドしてトリガーします。
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);