one function break onother. Is it possible?

Is it possible to stop one function by another one?

I want something like this :

 function foo()
 {
   bar();
   /*if is ok, then continue, else break*/
 }

 function bar()
 {
   if(!/**condition**/) stop foo();
 }

I know this is possible by using something like this :

  function foo()
  {
    if(!bar()) return;
  }

But maybe there is a better way?

Sample :

  var dk = new function() {
   this.doSomething() {
     this.em("some_id") || return;
   };

    this.em(data) { 
      return document.getElementById(data);
    };
  };

Answers


Inside your function, you could not tell the calling function to stop executing.

The second version you suggested is common practise for achieving this behaviour - let a function return true if it succeded and false if it didn't, then let the calling function decide what to do (i.e. break its execution).

Another approach might be throwing an exception. You should consider yourself what might be more useful in your situation


I think you already answered yourself..

bar() function could make all the checks or code you want then it return true or false to the calling function foo()

based on the returned value you decide either to continue your code execution or stopping it.


Need Your Help

"Insert Into" clause in sybase 15.5

insert sybase sybase-ase

I am inserting some records(~10k) into a temporary table using the insert into cluase.

Signal 11 SIGSEGV Crash in Galaxy S3 Android WebView

android html5 webview segmentation-fault galaxy

I have a complex, interactive HTML5 in an Android WebView - and it works fine on basically all platforms except Galaxy S3. On Galaxy S3 (Android 4.0.4), once out of every 5 times or so, just after...