-1

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.
 }
dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Jan 13, 2012 at 16:12
3
  • 2
    Can you explain why you don't want to do the nested definition/call that you describe? That's the obvious solution, so if you explain why it's not suitable for you it will help others create better answers. Commented Jan 13, 2012 at 16:14
  • 1
    can you elaborate? What are the conditions that require your nested function? alsocool is a local function within cool and can't be called outside of it. Either move alsocool out, or call it within. Those are your only choices that I see. Do you have a link or full sample to illustrate your constraints? Commented Jan 13, 2012 at 16:18
  • Sorry for not being clear. I've edit my question. Commented Jan 13, 2012 at 16:25

5 Answers 5

1

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();
 }
}
answered Jan 13, 2012 at 16:13
Sign up to request clarification or add additional context in comments.

4 Comments

Really? Isnt there any hacky way to pass a variable like: cool(abc); and then inside the function cool(), if abc passed, do the alsocool() ?
That's possible of course, but you explicitely said that you do not want to call it inside the function!
Exactly what I was looking for. Sorry for not being clear. I've edited my question.
Is it consider slow to do this?
1

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.
answered Jan 13, 2012 at 16:14

1 Comment

Thank you. It's what I was looking for. @ThieftMaster replied faster thus I will accept his answer. Thank you though.
1

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();
}):
answered Jan 13, 2012 at 16:14

1 Comment

Thank you for your answer and sorry I wasnt clear in the first place
1

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.

answered Jan 13, 2012 at 16:15

3 Comments

I understand that, thus I pass the cool() first and then with a way pass the alsocool(). For example: IF selector1 clicked, pass cool() and then alsocool(). If selector2 clicked, pass only cool()? isnt it possible?
@Jonathan: You can have cool return alsocool, and then call it needed. You can also pass the element to cool and let it figure out whether to call alsocool or not.
Sorry for not being clear. I've edited my question. What ThiefMaster answered I think its what I was looking for. Thank you
1

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();
});
answered Jan 13, 2012 at 16:16

2 Comments

I'd suggest returning a function instead of using arguments.callee.
I think its not what Rocket suggested. See ThieftMaster's answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.