closures

How is data passed to anonymous functions in JavaScript?

人走茶凉 提交于 2019-12-04 13:52:07
问题 When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000); } I get a "this.onTimeout is not a function"-error. I guess that 'this' is no longer available at the time the anonymous function is executing? So I've been doing this: MyClass.prototype.trigger = function(){ var me = this window.setTimeout(function(){me.onTimeout();},1000); } Is this really how you're supposed to do things? It kinda works, but it

Why use (function(){})() or !function(){}()?

纵然是瞬间 提交于 2019-12-04 13:18:19
I was reading In JavaScript, what is the advantage of !function(){}() over (function () {})()? then it hit me, why use : (function(){})() or !function(){}() instead of just function(){}() ? Is there any specific reason? It depends on where you write this. function(){}() by itself will generate a syntax error as it is evaluated as function declaration and those need names. By using parenthesis or the not operator, you enforce it to be interpreted as function expression , which don't need names. In case where it would be treated as expression anyway, you can omit the parenthesis or the operator.

JavaScript Closures Concerning Unreferenced Variables

依然范特西╮ 提交于 2019-12-04 13:04:25
问题 I'm aware of the great posts on Closures here and here, but neither seems to address the particular case I have in mind. The question is best demonstrated with code: function foo() { var x = {}; var y = "whatever"; return function bar() { alert(y); }; } var z = foo(); Referencing y within bar invokes a closure, and so long as I keep z around the garbage collector won't clean up y . The question is -- what happens to x ? Is it held by that closure too even though it doesn't get referenced?

Easiest way to build a tree from a list of Ancestors

北城余情 提交于 2019-12-04 12:34:03
问题 In my heart, I feel that there must be a super simple recursive solution to this, but I cannot immediately grok it. I have a tree stored in SQL as a closure table. The tree looks like: (1 (2 (3), 4)), and the languages are MySQL's SQL and PHP 5.3. The closure table is thus: +----------+------------+ | ancestor | descendant | +----------+------------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 4 | | 1 | 2 | | 1 | 3 | | 1 | 4 | | 2 | 3 | +----------+------------+ I can query the ancestors quite easily

Is a function return necessary to be called a Closure

柔情痞子 提交于 2019-12-04 12:26:29
问题 Hey i came across this video on youtube http://www.youtube.com/watch?v=KRm-h6vcpxs which basically explains IIFEs and closures. But what I am not understanding is whether i need to return a function in order to call it a closure. E.x. function a() { var i = 10; function b() { alert(i); } } in this case can i call it a closure as it is accessing the 'i' variable from the outer function's scope or do i need to return the function like this return function b(){alert(i);} 回答1: Returning the

Setting a variable in the closure scope

烈酒焚心 提交于 2019-12-04 11:20:36
I think I understand why variables exist outside of the function they were declared in, because you're returning another function: myFunction = function() { var closure = 'closure scope' return function() { return closure; } } A = myFunction(); // myFunction returns a function, not a value B = A(); // A is a function, which when run, returns: console.log(B); // 'closure scope' The way that it's written now, calling A() is like a getter. Q: How can I write myFunction so that calling A(123) is a setter? Try the following: myFunction = function() { var closure = 'closure scope' // value is

Why do javascript variables in closure functions not reset to a default when called multiple times?

随声附和 提交于 2019-12-04 10:50:14
In the code below please can someone explain to me why multiple calls to counter result in the value of i increasing each time it is called? My understanding is that as we specifically set i = 0; in makeCounter , each time makeCounter is called through the counter variable, i should be reset to 0. I cannot understand why this is not the case. function makeCounter() { // `i` is only accessible inside `makeCounter`. var i = 0; return function() { console.log( ++i ); }; } // Note that `counter` and `counter2` each have their own scoped `i`. var counter = makeCounter(); counter(); // logs: 1

How do I return a boxed closure from a method that has a reference to the struct?

Deadly 提交于 2019-12-04 10:44:07
I have a structure that contains a value and I want to obtain a function that operates on this value: struct Returner { val: i32, } impl<'a> Returner { fn get(&'a self) -> Box<Fn(i32) -> i32> { Box::new(|x| x + self.val) } } This fails compilation: error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements --> src/main.rs:7:18 | 7 | Box::new(|x| x + self.val) | ^^^^^^^^^^^^^^^^ | note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 5:1... --> src/main.rs:5:1 | 5 | impl<'a> Returner { | ^^^^^^^^^^^^^^^^^ = note: ...so that the types are

Should I pass a lambda by const reference.

徘徊边缘 提交于 2019-12-04 10:36:52
问题 Typically I use the following pattern when accepting a lambda as an argument to a function (A template class passed-by-value): template <class Function> void higherOrderFunction(Function f) { f(); } Does this copy (the closure of) the argument? If so, is there anything wrong with accepting the lambda by const reference instead? template <class Function> void higherOrderFunction(const Function& f) { f(); } A simple test seems to indicate that this works fine, but I want to know if there are

Exposing a method which is inside a closure

99封情书 提交于 2019-12-04 10:17:09
问题 When we are creating a method inside a closure it becomes private to that closure and can't be accessed until we expose it in some way. How can it be exposed? 回答1: You can return a reference to it... var a = function() { var b = function() { // I'm private! alert('go away!'); }; return { b: b // Not anymore! }; }; See it on jsFiddle. You could also bind it to the window object. But I prefer the method above, otherwise you are exposing it via a global variable (being a property of the window