开发者

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

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开发者_Python百科;
          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.


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
} 


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

Absolutely. It's called setTimeout(), but somehow I doubt that it would be a good solution for you.

Here it is anyway:

var foo = (function(){
  var b;

  return {
    bar: function(a) {
        if(b){
          setTimeout(function() {b = false;},20);
          return b;
        }else{
          b = a;
          return false;
        };
    }
  };
})();

foo.bar(1);

The function you passed as the first argument to setTimeout will "close around" the b variable, and set it after 20 milliseconds.

If you want to retain the synchronous flow of code execution, then absolutely not, unless you do it manually via a function that is returned along with the desired value.

Ultimately, your best bet will be the temp variable. You can close around it like the b variable if you wish:

var foo = (function(){
  var b,temp;

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

foo.bar(1);


it doesn't really matter what you set b to because you're declaring it with the var inside the function. It does not exist outside the function.


You really need to show your complete code here. In your example, b is always undefined, therefore the conditional would always enter the else case and secondly, since we got a lexical function scope in Javascript, b would always lose its value when the function ends.

So I guess you're taking advantage of closures to hold a reference to that variable. But its impossible to answer without seeing your actual code.


I don't understand anything about this code... but here you can do this :

function(a) {
    var b;
    // I suppose b is defined here ?

    if(b)
    {
        b = false;
        return !b;
    } else {
        b = a;
        return false;
    };
};


I think that this is not possible (or I don't know how to do it). I'll try to create a function that do whatever you want (i.e. reset variables) and then use return reset_variables();

The function 'reset_variables' also return the value what you want.


I don't think I would ever use something like this, but you could write a function for it:

var foobar = function(value, callback) {
  callback();
  return value;
};

Then it would be used like this in your code:

function(a) {
  var b;

  if (b) {
    // The first argument is what you want returned.
    // The second argument is a function that you want executed "after" you've calculated the return value
    return foobar(b, function() {
      b = false;
    });
  } else {
    b = a;
    return false;
  };
};

I called it foobar because I simply can't think of a name for this concept :)

Obviously the code does not actually execute after the actual return (that would be impossible) but it does capture the value of your return value before the final function is executed, which results in something that looks like what you're after.

But, once again, I'm not really sure if I'd advice to use something like this :)


You can use: Promise.resolve().then(() => console.log('your action'));

This is similar than using process.nextTick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜