closures

Possible Access To Modified Closure Issue… How To Beat?

橙三吉。 提交于 2020-01-03 19:31:06
问题 For some reason no matter what, the pageNumber ends up being the last value in the loop for the loopCounter. Now I would understand that if I were directly using loopCounter in the closure itself, but I'm not. As you can see from the code below, I am creating a new variable within the closure to take the current value of loopCounter. Only thing I can figure is (Assuming that javascript treats everything as a reference type) that pageNumber is taking the reference to loopCounter so no matter

How can I pass an argument to a function called using setTimeout?

我怕爱的太早我们不能终老 提交于 2020-01-03 11:13:09
问题 I want to pass an argument to a function called using setTimeout . I have found these three options: A = 1; // Method 1: closure things setTimeout(function() { whatsA(A); }, 100); // Method 2: third argument (same result with [A]) setTimeout(whatsA, 100, A); // Method 3: eval setTimeout('whatsA(' + A + ')', 100); A = 2; function whatsA(X) { console.log(X); } This shows 2 , undefined , and 1 in Internet Explorer 9. Method 1 : Clearly, I would not like the argument to be changed after passing

Javascript Closures and Variable Scope

天涯浪子 提交于 2020-01-02 10:21:37
问题 I'm extending my previous question, because I still don't fully understand the concept of javascript closures. Take a quick look at the following code, which will put two markers on a map. (The code is slightly modified from my previous question). var map = google.maps.somefunctoinstantiatemap(); var address = new Array(); address[0] = '1 Smith Street'; address[1] = '2 Smith Street'; function onpageload() { for(var rownum=0; rownum<address.length; rownum++) { geocoder.geocode({ 'address':

Same name (and signature) closure & function: non-ambiguous for >0 arguments, giving closure precedence when calling

偶尔善良 提交于 2020-01-02 09:54:40
问题 Precedence rules for same name and signature function & closure When defining a closure and a function with same name (say, foo ) and signature, it seems as if the closure takes precedence when calling said (seemingly ambiguous) foo . // Int -> () function func foo(num: Int) { print("function \(num)")} // Int -> () closure let foo: (Int) -> () = { print("closure \($0)")} /* or... let foo = { (num: Int) in print("closure \(num)")} */ foo(1) // closure 1 If I option-click the two declarations,

Auto-bind scope variables in a closure

谁说胖子不能爱 提交于 2020-01-02 08:24:10
问题 Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y = 456; return function(varname) { return eval(varname) } } closure = make_closure() closure("x") // 123 closure("y") // 456 The function returned from make_closure doesn't contain any references to scope variables, but still is able to return their values when called. Is there a way to do the same in python? def make_closure(): x = 123 return lambda varname: ...no "x" here... closure = make_closure()

Return a closure from a function

淺唱寂寞╮ 提交于 2020-01-02 08:13:08
问题 Note that this question pertains to a version of Rust before 1.0 was released Do I understand correctly that it is now impossible to return a closure from a function, unless it was provided to the function in its arguments? It is very useful approach, for example, when I need the same block of code, parameterized differently, in different parts of program. Currently the compiler does not allow something like this, naturally: fn make_adder(i: int) -> |int| -> int { |j| i + j } The closure is

overriding a function defined in jquery closure

浪子不回头ぞ 提交于 2020-01-02 07:29:26
问题 I have a function defined in jquery closure and called by another function in the same closure. Could I override the function being called without changing the closure itself. See the code for example (function($){ function Myfunction(value) { //do something with the value } $('a').live('click',function(){ MyFunction($(this).val()) }); }(JQuery)); is there a way I can override Myfunction so that overriden copy of Myfuntion is called inside event handler. 回答1: You can't. Functions defined

How to call non-escaping closure inside a local closure? [duplicate]

倖福魔咒の 提交于 2020-01-02 05:29:26
问题 This question already has answers here : Why do closures require an explicit `self` when they're all non-escaping by default in Swift 3? (2 answers) Closed 3 years ago . I have a function which looks something like this: func test(closure: () -> ()) { let localClosure = { closure() } localClosure() } This is only an example and does not fully reflect the problem I encountered, obviously here I could have just called closure directly! It should be clear that in the above code, closure cannot

Is there any difference between closure in Scheme and usual closure in other languages?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 03:53:06
问题 I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can

How to use indexesOfObjectsPassingTest: in Swift

柔情痞子 提交于 2020-01-02 03:49:25
问题 The declaration of indexesOfObjectsPassingTest: looks like this in Swift, func indexesOfObjectsPassingTest(predicate: ((AnyObject!, Int, CMutablePointer<ObjCBool>) -> Bool)!) -> NSIndexSet! I've tried all sorts of permutations to get this to work, but I'm stumped, particularly with how you deal with this piece, CMutablePointer<ObjCBool>) -> Bool)! . I found it confusing enough, when I was first learning blocks in objective-c, how you translate from the declaration to actual use of a method,