Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call outer function's return from inner function?

I have such code:

function allValid() {     $('input').each(function(index) {         if(something) {             return false;          }         });      return true; } 

which always returns true as return false; affects anonymous inner function. Is there an easy way to call outer function's return?

PS. I am not looking for a workaround, just want to know the answer to original question. If the answer is "not possible" it is fine.

like image 862
serg Avatar asked Mar 24 '10 16:03

serg


People also ask

Can you call a nested function outside a function?

Nested function is private to containing function Only the containing function can access the nested function. We cannot access it anywhere outside the function. This is because the inner function is defined in the scope of the outer function (or containing function).

Can inner function access outer variable JavaScript?

A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables — a scope chain. The closure has three scope chains: it has access to its own scope — variables defined between its curly brackets. it has access to the outer function's variables.

What does return mean in JavaScript?

The return statement is used to return a particular value from the function to the function caller. The function will stop executing when the return statement is called. The return statement should be the last statement in a function because the code after the return statement will be unreachable.


1 Answers

Yeah, store it in a local variable.

function allValid() {   var allGood = true;   $('input').each(function (index) {     if (something) {       allGood = false;     }   });    return allGood; } 
like image 194
bcherry Avatar answered Sep 29 '22 11:09

bcherry