closures

Click Listeners in Loop - Array and Closure

别说谁变了你拦得住时间么 提交于 2019-12-02 20:37:07
问题 I realise I'm treading on thin ice opening another closure issue, but I have searched and can't find an answer to my issue. I have a Google Maps API v3 page which generates two maps from one block of code - a small map centered on the user's current location and a larger map showing the whole area with the user's location marked where it is, center or not. On top of the map is a rectangle layer consisting of 14 rectangles. In order to generate the two maps, I have had to put the rectangles in

Closures vs. classes for encapsulation?

北城余情 提交于 2019-12-02 20:28:21
I'm new to JS (from C++/etc), and it's just occurred to me that closures seem to be a simpler and more convenient way to handle encapsulation than classes. This code seems to give a simple way to handle encapsulation: function addProperty(o) { var value; o["get"] = function() { return value; } o["set"] = function(v) { value = v; } } // create two independent objects which are instances of pseudo-class 'addProperty', // which each have their own version of a set of local fields, and methods that // operate on those fields: var a = {}; addProperty(a); var b = {}; addProperty(b); If you just want

Why do Python yield statements form a closure?

匆匆过客 提交于 2019-12-02 20:24:56
I have two functions that return a list of functions. The functions take in a number x and add i to it. i is an integer increasing from 0-9. def test_without_closure(): return [lambda x: x+i for i in range(10)] def test_with_yield(): for i in range(10): yield lambda x: x+i I would expect test_without_closure to return a list of 10 functions that each add 9 to x since i 's value is 9 . print sum(t(1) for t in test_without_closure()) # prints 100 I expected that test_with_yield would also have the same behavior, but it correctly creates the 10 functions. print sum(t(1) for t in test_with_yield()

capture by value class members

痞子三分冷 提交于 2019-12-02 20:20:52
Is there a way, when writing a lambda function within a member function, to capture fields of the enclosing class by value? The default catch-all = doesn't work because when I reference the variable inside the lambda I get instead a dereferencing from the captured this pointer, as well as explicitly naming the variable in the capture list, because I get two compile error: capture of non-variable <name> , and ‘this’ was not captured for this lambda function No, data members cannot be captured by value. A lambda can capture only two kinds of things: the this pointer, and nonstatic local

Javascript - difference between namespace vs. closure?

≡放荡痞女 提交于 2019-12-02 19:43:38
In Javascript, what's the difference between a namespace and a closure? They seem very similar to me. EDIT Specifically, this article discusses namespaces and closures, and has sentences like Now, we’re still going to have situations where we’ll want to declare variables that don’t naturally fit into a namespaced object structure. But we don’t want those variables to have a global scope. This is where self-invoking functions come in. It goes on to give what looks a lot like a closure, as an "object namespace". It looks to me like the namespace IS a closure ...but maybe it's not...? Help? A

Scope chain look-up vs prototype look-up - Which is when

China☆狼群 提交于 2019-12-02 19:36:11
If a variable is not available in a function when it's needed, then it's being looked for in the scope chain (which is a closure), but other times it's being searched for in the prototype chain. I am trying to wrap my head around which is happening when. I was wondering if someone could kindly clear the mist for me, or refer me to some literature discussing this topic specifically. For example, would I be right saying that: - Objects and therefore public variables tied to the context (this)are always looked up in the prototype chain? - Private variables are always looked up in the scope chain

Javascript Closure Problem

て烟熏妆下的殇ゞ 提交于 2019-12-02 18:51:55
问题 I know this kind of question gets asked alot, but I still haven't been able to find a way to make this work correctly. The code: function doStuff () { for (var i = 0; i< elementsList.length; i++) { elementsList[i].previousSibling.lastChild.addEventListener("click", function(){ toggle(elementsList[i])}, false); } } // ends function function toggle (element) { alert (element); } The problem is in passing variables to the toggle function. It works with the this keyword (but that sends a

What is so special about closures?

蓝咒 提交于 2019-12-02 18:28:33
I've been reading this article about closures in which they say: "all the plumbing is automatic" the compiler "creates a wrapper class" and "extends the life of the variables" "you can use local variables without worry" the .NET compiler takes care of the plumbing for you, etc. So I made an example based on their code and to me, it seems as though closures just act similarly to regular named methods which also "take care of the local variables without worry" and in which "all the plumbing is automatic". Or what problem did this "wrapping of local variables" solve that makes closures so special

iOS Swift: Closures (Callbacks) versus Delegates, when to use which? [closed]

依然范特西╮ 提交于 2019-12-02 18:14:28
Personally I prefer callback over delegate in Swift for simple logical correlations, because it's pretty straight-forward and easy to understand. At the same time, some prefers delegate, since delegation is a popular pattern in other languages, such as C#. There are some discussions I found online: 1. "Why you shouldn't use delegates in Swift?" https://medium.cobeisfresh.com/why-you-shouldn-t-use-delegates-in-swift-7ef808a7f16b#.hqb7zrc1v 2. Apple is shifting its focus more on the callback pattern https://www.reddit.com/r/swift/comments/2ces1q/closures_vs_delegates/ 3. blocks or delegates?

Can someone explain this Javascript closure example

核能气质少年 提交于 2019-12-02 18:11:43
问题 function makeAdder(x) { return function(y) { console.log("X:" + x + " Y:" + y); return x + y; }; } var add5 = makeAdder(5); var add10 = makeAdder(10); console.log(add5(2)); console.log(add10(2)); Ok, I am a bit confused with this example on developer.mozilla under closures. If someone can explain with as much detail as possible in order for me to get my head around closures. Ignore the console.log, I just added that to see what values are displayed and from that I can see that x is 5 and y is