0

here is the code

test ('e');
function test(e) {
    test2(e); //undefined
}

(function ($) {
    function test2(e) {
        alert('test');
    }
})

Because of something restriction, i have to call like this. Anyone knows?

3 Answers 3

1

You can't, the function test2 has been defined in the closure, you can only call the function within that scope.

Sign up to request clarification or add additional context in comments.

Comments

0

You can, by declaring test2 outside the anonymous function:

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.

Demo: http://jsfiddle.net/DerekL/LEZkt/

1 Comment

得閒解答吓人哋D問題…如果你想上SO吹水嘅話可以嚟我哋開嘅chat room
0

bind event on document and trigger it:

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);

http://jsfiddle.net/oceog/CzNKu/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.