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
Post a Comment