last day (15 days later) » 

04:19
-1
Q: Closures in Javascript - unnecessary overhead or a neccessity?

Alok DI generally avoid using closures. I still am unable to understand what closure achieves. For closures - to comprehend the code is a large environment is a lot more cumbersome. Two popular examples of closure are: 1. the event handler example like the one here: Understanding javascript closure ...

"the function func1() is created using a reference to values in the scope of parents.", "just because an object's parent method has returned doesn't mean the child method has "lost everything"" - yes, that's what closures are all about. But no, this behaviour does not follow from functions being objects.
"I generally avoid using closures perhaps because I always found the oldest ES4 spec but that was the simplest one..." - I don't understand what you're talking about. Closures were part of JavaScript since ES1, and they are a necessity in any reasonable application. Sure you can program without them, but it's very cumbersome.
or is there something more about it? nope.
Thanks Bergi, but they are still attaching the named function to the parent to "window" object as a method of the window object, is it not?
@AlokD "they are still attaching the named function to the parent to "window" object" - what? Who? Where?
Btw, no, "a reference to the function func1 with the variable values at "that time of return" plugged in" is not what is happening. If you plugged in the values, how would ref() and ref() return different values? The closure has a reference to the variables plugged in, and it can alter those variables.
Because the parent object still exists
04:19
tl;dr: How can function access values? Usually as parameters, i.e. the values a function needs are passed to it at call time. That is all good and fine if you have full control how and when a function is called. But in JavaScript functions are very often used as callbacks, i.e. you do not control when and how a function is called. So you need another to provide values that the function needs and the concept of closures does this "naturally". Btw, in theory every function is a closure in JavaScript, whether you use it as such or not.
just added and edit to further illustrate my understanding. I mean you could do a new instance of the function everytime and use the inner one as public and still get the same result - because the function is also an object @Felix, I actually get "lost" in closures, especially in the callback or event handler case - and always prefer adding an attribute to the DOM that can be used in the event handler. Most articles talking about the usage of closure actually use the return value being a function reference, show me a case without that return value being a function.
"t is simply returning the function with the variables from the parent scope "plugged in as they were when the return was called." No, free variables are evaluated when the "closure function" is called not when it is created. Simple example var closure = (() => { let x = 0; setTimeout(() => x = 1, 0); return () => console.log(x); })(); setTimeout(closure, 0);
What you have is just a constructor function. That doesn't really having anything to do with closures or the problem they solve.
....Again you are returning a function, closure or no closure that is making the difference... return () => I also see you are using "let"
This is just an example. Whether or not I return a function is irrelevant. Whether I use let is irrelevant. Here is an even simpler example: var x = 1; function foo() { console.log(x);}; x = 2; foo();. "always prefer adding an attribute to the DOM" No all event handling has to do with the DOM. Think about Ajax requests...
var x = 1; function foo() { console.log(x);}; x = 2; foo(); This follows from the lex itself, not "closure". foo is called when x=2 function timer(x) { return function(){ console.log(x);} } function init() { for (var i = 1; i <= 2; i++) { setTimeout(timer(i), 10000); } } Works fine too.. because of the return function approach.. all I am saying if functions are objects, defining a new term called closure has no meaning because that is how objects would behave anyways, Need an example without the return function(){..} approach
04:19
It is a closure. How do you think the function has access to variable x? "all I am saying if functions are objects, defining a new term called closure has no meaning because that is how objects would behave anyways" Closures have nothing to with objects. Closures exist in other languages where functions are not objects, e.g. PHP. It's an abstract programming/language concept. Besides, you are conflating a couple of things: Yes, functions are objects, but when you call new func() then you are creating a new plain object. It has nothing to do with functions being objects.
"The fact that a function is an object itself implies closure behavior." No, it does not, at all. JavaScript could implement closures without functions being objects and vice versa (i.e. have functions not be closures). Maybe you don't quite know what a closure is: A closure is a function that has a reference to the environment it was created in and it uses that environment to resolve free variables. Nothing in that sentence requires any "objects" (whatever "objects" are in an abstract context).
Look at the language specification. Step 5 is what enables the closure behavior. If just leave that step out, then functions would not be closures. So, closures are not a consequence of functions being objects, it's an explicit decision that the language designers made, which is independent from making functions objects.
Thanks a lot, do you have a specific example without that return function (){...} approach that can be referred to for a better understanding? A lot of what closure is comes from basic inheritance and scope (lexical) as I see it, but yes the rules are in the specification.
I don't quite understand what your problem with return function() {...} is, but I have provided two examples that demonstrate closure behavior one with return and one without. And if your argument is that the second is not a closure than I have to say: it is. If you consider the same example in PHP: $x = 1; function foo() { echo $x; }; foo(); then you'd get an error, because $x is not defined inside foo. Being a closure allows a function to access variables that are not defined "in itself". That makes the example in JavaScript a closure.
PHP has explicit syntax for creating closures which would look like this: $x = 1; function foo() use ($x) { echo $x; }; foo();. "A lot of what closure is comes from basic inheritance and scope (lexical) as I see it" Yes, kind of. Basically, all the nested lexical scoped are linked. So if a variable is looked up but doesn't exist in one scope, the falls back to looking it up in the "parent scope". But you should not confused that with traditional inheritance in OOP. The scopes are just linked one way or the other.
PHP>=5.3 does support variable closures (global and ocal scope) via use and global. But not functions. All functions outer or inner are global from what I recollect. Which is why I am asking for a non return function(){...} approach. That helps understand the idea better. From a Theory of CS view point, yeah, the wiki says it's a record of what variables get mapped where and we define those rules in the language specification, but then it's equally valid to say "PHP closures behave this way for functions" . Or does "something being a closure imply that a record exists for that mapping"??
By the way after 5.3 the anonymous functions in PHP are also called a closure too, but it isn't really s closure from a scoping view point. So what does closure mean? That a record exists at runtime of the mappings?
"So what does closure mean?" I explained that earlier: "A closure is a function that has a reference to the environment it was created in and it uses that environment to resolve free variables." And an environment is just a mapping of variable names to variable values. One way of implementing global scope (and how it is done in JavaScript) is to have every function be a closure and have the global scope at the end of the scope chain that the function closes over. Because that means that every function has access to that scope.
But variables were mapped to environment prior to PHP5.3 too, but most people say "closures were introduced in 5.3 in PHP". Why so? The mappings were different yes, but there were mappings

last day (15 days later) »