closures

What's the difference between closure parameters and the 'use' keyword?

谁都会走 提交于 2019-12-18 14:14:40
问题 This has got me very confused and I can't seem to find an answer to this question. A clear and simple clarification would be nice. 回答1: A closure is a function that is evaluated in its own environment, which has one or more bound variables that can be accessed when the function is called. They come from the functional programming world, where there are a number of concepts in play. Closures are like lambda functions, but smarter in the sense that they have the ability to interact with

Why are self-executing anonymous functions used in Javascript Module pattern?

纵饮孤独 提交于 2019-12-18 13:16:17
问题 In the module pattern in JavaScript "Immediately-Invoked Function Expressions" (also known as self-executing anonymous functions) are used as self executing functions that return an object. How can a self-executing function hide private variables and only expose the returned object. Why does this not happen with a normal JavaScript function? So in the following mini module, why could we not achieve the same concept of encapsulation without the enclosing ()()? var Module = (function () { var

Is it possible to define a Ruby singleton method using a block?

白昼怎懂夜的黑 提交于 2019-12-18 13:14:25
问题 So, I want to define a singleton method for an object, but I want to do it using a closure. For example, def define_say(obj, msg) def obj.say puts msg end end o = Object.new define_say o, "hello world!" o.say This doesn't work because defining a singleton method via "def" is not a closure, so I get an exception that "msg" is an undefined variable or method. What I would like to do is something like using the "define_method" method in the Module class, but as far as I can tell, this can only

Testing within a javascript closure

一笑奈何 提交于 2019-12-18 12:56:11
问题 Is it possible to unit test javascript functions that exist within a closure, so for example, given the following: (function() { var a = function() { //do something } window.b = function() { // do something else } })(); Is it possible to unit test function a without exposing it? If not, is there a good way to expose a, but only in test mode? 回答1: Your anonymous function could take a parameter which would be undefined when not in test mode, and say this parameter would be an object, you could

Java Lambdas and Closures

非 Y 不嫁゛ 提交于 2019-12-18 11:49:07
问题 I hear lambdas are coming soon to a Java near you (J8). I found an example of what they will look like on some blog: SoccerService soccerService = (teamA, teamB) -> { SoccerResult result = null; if (teamA == teamB) { result = SoccerResult.DRAW; } else if(teamA < teamB) { result = SoccerResult.LOST; } else { result = SoccerResult.WON; } return result; }; So right off the bat: Where are teamA and teamB typed? Or aren't they (like some weird form of generics)? Is a lambda a type of closure, or

JavaScript Closures - Using the ECMA Spec, please explain how the closure is created and maintained

Deadly 提交于 2019-12-18 11:43:00
问题 I'm reading about JavaScript closures. I'm familiar with Execution Contexts, how the Lexical Environment is maintained, and very familiar with Lexical Scoping. I want to know how closures in JavaScript are created and maintained . Sometimes it's hard for me to grasp such important concepts without knowing how it is actually doing it. I know that, according to Wikipedia, a closure is is a function or reference to a function together with a referencing environment—a table storing a reference to

Self Executing functions in PHP5.3?

不羁岁月 提交于 2019-12-18 11:10:30
问题 I was trying to borrow some programing paradigms from JS to PHP (just for fun). Is there a way of doing: $a = (function(){ return 'a'; })(); I was thinking that with the combination of use this can be a nice way to hide variables JS style $a = (function(){ $hidden = 'a'; return function($new) use (&$hidden){ $hidden = $new; return $hidden; }; })(); right now I need to do: $temp = function(){....}; $a = $temp(); It seems pointless... 回答1: Function Call Chaining, e.g. foo()() is in discussion

javascript closure advantages?

百般思念 提交于 2019-12-18 10:49:11
问题 Whats the main purpose of Closures in JS. Is it just used for public and private variables? or is there something else that I missed. I am trying to understand closure and really want to know what are the main advantages of using it. 回答1: I think the best phrase to sum up the purpose of closures would be: Data Encapsulation With a function closure you can store data in a separate scope, and share it only where necessary. If you wanted to emulate private static variables , you could define a

Recursive closures in JavaScript

早过忘川 提交于 2019-12-18 10:40:35
问题 Let's say I have something like function animate(param) { // ... if (param < 10) setTimeout(function () { animate(param + 1) }, 100); } animate(0); Does this mean each instance of the function's local data will be held in memory until animate completes, i.e. until param reaches 10? If it's true that instances are held in memory, is there a better way of doing this? I know, passing textual code to setTimeout() solves the problem but in my case there are objects among function arguments that

Do we have closures in C++?

大兔子大兔子 提交于 2019-12-18 10:27:08
问题 I was reading about closures on the net. I was wondering if C++ has a built-in facility for closures or if there is any way we can implement closures in C++? 回答1: The latest C++ standard, C++11, has closures. http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions http://www.cprogramming.com/c++11/c++11-lambda-closures.html 回答2: If you understand closure as a reference to a function that has an embedded, persistent, hidden and unseparable context (memory, state), than yes: