Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript, is there a technique where I can execute code after a return?

Tags:

javascript

Is there a technique where I can execute code after a return? I want to return a value then reset the value without introducing a temporary variable.

My current code is:

var foo = (function(){
  var b;

  return {
    bar: function(a) {
        if(b){
          var temp = b;
          b = false;
          return temp;
        }else{
          b = a;
          return false;
        };
    }
  };
})();

foo.bar(1);

I want to avoid the temp var. Is that possible?

var b holds a value between function calls because it is a memoization styled function.

like image 579
Christopher Altman Avatar asked Feb 01 '11 15:02

Christopher Altman


1 Answers

Use a finally block to ensure certain code runs after another block. That block can include errors, returns, or whatever else. The finally block will run after.

try {
    return 'something';
} finally {
    // your after return code
} 
like image 88
Lucian Sturião Avatar answered Sep 18 '22 13:09

Lucian Sturião