javascript - Nested "Return" to Exit Parent Function? -


my goal check condition , exit out of current function. however, prefer in function called function want exit. simple example not call separate function , check condition in body:

$scope.dostuff = function(){   if (something) {       return;   }   dosomething(); } 

can part below...

  if (something) {       return;   } 

...be placed in function can used in dostuff(), so?

$scope.dostuff = function(){   $scope.exitoncondition();   dosomething(); }  $scope.exitoncondition){       if (something) {           return;       } } 

obviously in way wrote it, "return" return out of exitoncondition function, not dostuff. usual, don't need code checked, general example, here illustrate question.

have exitoncondition return boolean, , call in if statement.

$scope.dostuff = function(){   if ($scope.exitoncondition())     return;   dosomething(); }  $scope.exitoncondition = function(){       if (something) {           return true;       } } 

to avoid return in main function, restructure little if need stay.

$scope.dostuff = function(){   if (!$scope.exitoncondition())     dosomething(); }  $scope.exitoncondition = function(){       if (something) {           return true;       } } 

notice ! negation of result. can little cleaner if reverse meaning of exitoncondition() function.

$scope.dostuff = function(){   if ($scope.passedcondition())     dosomething(); }  $scope.passedcondition = function(){       if (something) {           return false;       } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -