admin管理员组

文章数量:1392068

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.

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.

Share Improve this question edited Feb 1, 2011 at 16:37 Christopher Altman asked Feb 1, 2011 at 15:18 Christopher AltmanChristopher Altman 4,8963 gold badges35 silver badges50 bronze badges 6
  • temp will always be true in the first condition.... – Caspar Kleijne Commented Feb 1, 2011 at 15:29
  • "var b holds a value between function calls" - No it doesn't. – OrangeDog Commented Feb 1, 2011 at 15:51
  • It seems that nobody noticed that the function declaration above is missing its name :) – Šime Vidas Commented Feb 1, 2011 at 15:54
  • Updated the sample code to clarify the question – Christopher Altman Commented Feb 1, 2011 at 16:01
  • @Šime Vidas: You assumed the anonymous function was intended to be a declaration. :) – user113716 Commented Feb 1, 2011 at 16:07
 |  Show 1 more comment

8 Answers 8

Reset to default 12

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

本文标签: In JavaScriptis there a technique where I can execute code after a returnStack Overflow