Baylock
Baylock

Reputation: 1260

Reach a nested javascript function from another one (beginner inside)

I use a slideshow system I found over the internet and I try to tweak it a little bit. There are two js files there and each one of them is made of functions like this:

(function($) {
    $.file1 = function(data, vData) 
    {
       //function aaa
       //function bbb
       //function ccc
    }
})(jQuery);

(function($) {
    $.file2 = function(data, vData) 
    {
       //function eee
       //function fff
       //function gig
    }
})(jQuery);

How can I trigger function eee from inside function bbb?

I tried these from inside the bbb function but it didn't work:

eee();
$.file2.eee();

I'm not trying to return a variable, I just need the function triggered.

Thank you for your help.

Upvotes: 0

Views: 279

Answers (3)

Baylock
Baylock

Reputation: 1260

I would like to thank you all for your help.

What I did is this: Inside the calling function bbb (in file 2), I added this:

$.file1.triggerEee();

Inside the main function of file 1 ($.file1) I added this:

$.file1.triggerEee=function(){eee();};

It did the trick.

Thank you very much.

Upvotes: 1

jbabey
jbabey

Reputation: 46657

You would need to expose the functions to other scopes:

(function($) {
    $.file1 = function(data, vData) {
       //function aaa
       //function bbb
       //function ccc
       return {
           aaa: aaa,
           bbb: bbb,
           ccc: ccc
       };
    }
})(jQuery);

$.file1().aaa();
$.file1().bbb();
$.file1().ccc();

Note that this is a pretty awkward pattern for defining functions. It's not entirely clear what you're trying to accomplish with this pattern. You might be better off using an IIFE:

var thing = (function($) {
    //function aaa
    //function bbb
    //function ccc

    return {
        aaa: aaa,
        bbb: bbb,
        ccc: ccc
    };
})(jQuery);

thing.aaa();
thing.bbb();
thing.ccc();

Upvotes: 1

jos
jos

Reputation: 431

inside those two function jquery calls just reference the methods, don't implement it there. If you implement it elsewhere it will be extremely easy to reference.

Upvotes: 0

Related Questions