There is a method in Javascript where can I execute the code after returning?

Is there a way when I can execute the code after returning? I want to return a reset 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 temp var. Is it possible?

var b contains a value between function calls, as it is a reminder-like function.

+4
source share
7 answers

There is a method in Javascript where can I execute the code after returning?

That's right. It was called setTimeout() , but for some reason I doubt it would be a good solution for you.

One way or another:

 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 that is passed as the first argument to setTimeout will "close around" the b variable, and set it after 20 milliseconds.

If you want to keep a synchronous stream of code execution, then absolutely not, unless you do it manually through a function that returns along with the desired value.

Ultimately, temp best option. You can close it, like the b variable, if you want:

 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); 
+3
source

Use the finally block to ensure that certain code executes after another block. This block may include errors, returns, or something else. The finally block will work after.

 try { return 'something'; } finally { // your after return code } 
+3
source

it doesn't really matter that you set b because you declare it with var inside the function. It does not exist outside a function.

+2
source

You really need to show the full code here. In your example, b always undefined , so the condition will always introduce an else case , and secondly, since we got the area of ​​the lexical function in Javascript, b always loses its value when the function ends.

So, I assume that you are using closure to reference this variable. But it is impossible to answer without seeing your actual code.

+2
source

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; }; }; 
0
source

I don’t think that I will ever use something like this, but you can write a function for it:

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

Then it will be used as 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 just can’t think of a name for this concept :)

Obviously, the code really does not execute after the actual return (this would not be possible), but it captures the value of the return value until the last function is executed, which leads to what it looks like you are.

But, again, I'm not sure if I recommend using something like this :)

0
source

I think this is impossible (or I do not know how to do this). I will try to create a function that will do anything (i.e., reset variables), and then use return reset_variables();

The function 'reset_variables' also returns the value you want.

0
source

Source: https://habr.com/ru/post/1337962/


All Articles