closures

Passing values to onclick [duplicate]

混江龙づ霸主 提交于 2019-12-17 06:14:17
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . If I create a whole lot of HTML elements using a loop, like for (i= 1; i < 100; i++) { var my_element = document.createElement ("td"); row.appendChild (my_element); my_element.onclick = function () {my_function (i)); } then when the element is clicked, the value of i passed to my_function is always 100, regardless of what number element is calling it. I have

Can Swift return value from an async Void-returning block?

不想你离开。 提交于 2019-12-17 06:13:27
问题 I want to create a function to check if user_id is already in my database. class func checkIfUserExsits(uid:String) -> Bool { userRef.childByAppendingPath(uid).observeSingleEventOfType(.Value, withBlock: { (snapShot: FDataSnapshot!) -> Void in if snapShot.value is NSNull { return false } else { return true } }) } However, observeSingleEventOfType is a API provided by 3rd party Firebase. It is defined to return Void . (void)observeSingleEventOfType:(FEventType)eventType withBlock:(void ( ^ ) (

javascript closure immediate evaluation [duplicate]

大憨熊 提交于 2019-12-17 05:12:47
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . Consider the following Javascript code: var a = []; var f = function() { for (var i = 0; i < 3; i++) { a.push(function(){alert(i)}); } for (var j = 0; j < 3; j++) { a[j](); } }; The alerts print out '3' all three times. I want a different behaviour - in each iteration of the loop generate a function that prints the current value of i. I.e. 3 functions that

javascript closure immediate evaluation [duplicate]

↘锁芯ラ 提交于 2019-12-17 05:12:25
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . Consider the following Javascript code: var a = []; var f = function() { for (var i = 0; i < 3; i++) { a.push(function(){alert(i)}); } for (var j = 0; j < 3; j++) { a[j](); } }; The alerts print out '3' all three times. I want a different behaviour - in each iteration of the loop generate a function that prints the current value of i. I.e. 3 functions that

Instance Method Reference and Lambda Parameters

拟墨画扇 提交于 2019-12-17 05:11:12
问题 I am having trouble understanding the syntax for a method reference, where there are two parameters a and b , and the reference is to a method of a on b . For example I understand how Arrays.sort(personArray, comparators::compareByName); is equivalent to Arrays.sort(personArray, (o1, o2) -> comparators.compareByName(o1, o2)); because in that case the lambda parameters match the method call parameters (o1, o2) . Howevever for this lambda stream.sorted((o1, o2) -> o1.compareToIgnoreCase(o2));

setTimeout() inside JavaScript Class using “this”

旧巷老猫 提交于 2019-12-17 05:01:21
问题 I am trying to use setTimeout() inside a class function in JavaScript. The setTimeout() is supposed to trigger another method in the same Class, so the function I am passing it is written as window.setTimeout("this.anotherMethod", 4000) . That bring the problem: this references the calling Object, in the case of setTimeout() it is window . How can I use enclosures to return a reference to the Class Object itself? myObject = function(){ this.move = function(){ alert(this + " is running"); }

Closures in a for loop

大憨熊 提交于 2019-12-17 04:33:55
问题 Closures in a loop are causing me problems. I think I have to make another function that returns a function to solve the problem, but I can't get it to work with my jQuery code. Here is the basic problem in a simplified form: function foo(val) { alert(val); } for (var i = 0; i < 3; i++) { $('#button'+i).click(function(){ foo(i); }); } Naturally clicking on any of the three buttons will give an alert saying 3. The functionality I want is that clicking on button 1 will give an alert saying 1,

Closures in a for loop

瘦欲@ 提交于 2019-12-17 04:33:07
问题 Closures in a loop are causing me problems. I think I have to make another function that returns a function to solve the problem, but I can't get it to work with my jQuery code. Here is the basic problem in a simplified form: function foo(val) { alert(val); } for (var i = 0; i < 3; i++) { $('#button'+i).click(function(){ foo(i); }); } Naturally clicking on any of the three buttons will give an alert saying 3. The functionality I want is that clicking on button 1 will give an alert saying 1,

Why results of map() and list comprehension are different?

流过昼夜 提交于 2019-12-17 04:32:55
问题 The following test fails: #!/usr/bin/env python def f(*args): """ >>> t = 1, -1 >>> f(*map(lambda i: lambda: i, t)) [1, -1] >>> f(*(lambda: i for i in t)) # -> [-1, -1] [1, -1] >>> f(*[lambda: i for i in t]) # -> [-1, -1] [1, -1] """ alist = [a() for a in args] print(alist) if __name__ == '__main__': import doctest; doctest.testmod() In other words: >>> t = 1, -1 >>> args = [] >>> for i in t: ... args.append(lambda: i) ... >>> map(lambda a: a(), args) [-1, -1] >>> args = [] >>> for i in t: ..

Is window really global in Javascript?

本秂侑毒 提交于 2019-12-17 04:29:15
问题 Take this piece of Javascript in a browser: <script> console.log(window.someThing); var x = 12; function foo() { window.otherThing = x; } </script> Inside foo we can access window , we all know that, but why exactly? Is it some kind of special global variable? Or does the "root scope" (inside the script tag) have it as an implicit local variable and is it simply "closure-inherited" as any other local variable (like x above) can be? And how does that concur with variables declared directly