jQuerybeast
jQuerybeast

Reputation: 14490

Run a function inside a function

I have this:

function cool(){
   function alsocool(){

   }
}

And I run the cool() on button click:

$(selector).on('click', function(){
 cool();
}

How can I run the cool() and the alsocool() from the same click? Note that I don't want to do:

function cool(){
   function alsocool(){

   }
   alsocool();
}

If I do :

$(selector).on('click', function(){
     cool(); alsocool();
    }

it doesn't work.

Is it possible to run a function inside a function on the same call?

EDIT:

I DO WANT to pass cool() since obviously alsocool() is not recognized once its inside function cool() BUT cool(); is passed from many selector thus I want to know from which selector is passed and take the appropriate action.

Example I want something like this:

function cool(){

// If this was called by button1, run alsocool() else bypass it
       function alsocool(){

       }
// some code goes here

}

$("#button1").on('click', function(){
         cool(); alsocool();
         // If button1 clicked, run cool AND alsocool
        }
$("#button2").on('click', function(){
         cool(); // If button2 clicked, run cool ONLY.
    }

Upvotes: -1

Views: 233

Answers (5)

Francis
Francis

Reputation: 3383

I don't understand why you want to do that, but you can do this :

function cool()
{
    arguments.callee.alsoCool = function() {
        alert("also cool");
    };

    alert("cool");
}

$("#b").click(function() {
    cool();
    cool.alsoCool();
});

Live demo: http://jsfiddle.net/ENqsZ/

Alternatively, as a Rocket suggested, you can do this :

function cool()
{
    alert("cool");

    return function() {
        alert("also cool");
    };
}

$("#b").click(function() {
    var alsoCool = cool();

    alsoCool();
});

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227220

You can't do that. alsocool only exists inside cool, the click handler has no idea alsocool exists.

If you don't want to call alsocool from inside cool, then you're gonna have to make alsocool global.

Upvotes: 1

Marc B
Marc B

Reputation: 360612

If you do

function cool() {
   function alsocool() { ... }
}

Then 'alsocool' only exists while the cool() function is executing. It will not be externally accessible.

You'd want:

function cool() { ... }
function alsocool() { ... }

$(selector).click(function() {
   cool();
   alsocool();
}):

Upvotes: 1

Matt
Matt

Reputation: 75317

The problem is that because you've defined the function alsocool within cool, it's visibility is limited to that scope.

Because of this, you can only call the function alsocool from within cool.

You can, of course, move the declaration of alsocool outside of cool, and this will still allow you to call alsocool from within cool, but you will loose access to the scope of cool from within alsocool.

You could also limit the invocation of alsocool inside cool depending on a parameter passed, if this is a viable option for you;

function cool(alsoAlsoCool){
   function alsocool(){

   }

   if (alsoAlsoCool) {
       alsocool();
   }
}

// cool(true) will call it, but cool() or cool(false) won't.

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318498

The answer is simple: It is impossible.
The inner function is local to the containing function's scope so unless that function calls it, it cannot be called at all.

If you want both functions to be reachable from outside, define alsocool outside cool, i.e. on the same level as cool.


As per your comment, here's a way that would use a parameter to determine if the inner function should be called or not:

function cool(callInner){
    function alsocool(){

    }
    if(callInner) {
        alsocool();
    }
}

Upvotes: 1

Related Questions