Comments on: Closures in JavaScript https://j11y.io/javascript/closures-in-javascript/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Johan https://j11y.io/javascript/closures-in-javascript/#comment-1133 Fri, 19 Feb 2010 20:07:03 +0000 https://j11y.io/?p=1017#comment-1133 I’m sorry, I don’t usually flame people on the internet. I’ll make an exception this time 🙂

Dusan:

“But I am simply wondering: Why are people reading this blog?”
They find it informing, and as you said, it’s well written. Perhaps you in your great experience could produce something of more interest.

“Why am I reading a blog of an 18 year old “scripter” ?”
Since when did age become of importance. I have a long time M.sc. in computer science, still I learned something reading this article. Your arrogance is your greatest flaw.

“And on top of that, all is about one poorly made scripting language … Amazing.”
You said yourself you’re using it alot. What does that make you?

]]>
By: James Rourke https://j11y.io/javascript/closures-in-javascript/#comment-1132 Sun, 15 Nov 2009 23:29:01 +0000 https://j11y.io/?p=1017#comment-1132 It does seem extremely arrogant of them. Initially I thought Dean Edwards was being sarcastic, and the first paragraph would still seem sarcastic if not for the second one. Quite surprising really. Why shouldn’t an 18 year-old kid write about what he thinks closures are about, inviting others to contribute to a discussion? After all, the way we imagine certain things in a programming language (like Objects) is worth sharing with each other.

]]>
By: Guillermo Rauch https://j11y.io/javascript/closures-in-javascript/#comment-1131 Sun, 15 Nov 2009 21:55:40 +0000 https://j11y.io/?p=1017#comment-1131 I don’t understand Dean Edwards’ or Dusan’s comments. How does the guy age play a role in the analysis of the article? How can you question whether he should be blogging or not when you’re both saying that the information is technically correct?

]]>
By: Nicolaj Kirkgaard Nielsen https://j11y.io/javascript/closures-in-javascript/#comment-1130 Wed, 19 Aug 2009 13:31:06 +0000 https://j11y.io/?p=1017#comment-1130 When trying to encapsulate the jQuery function like @mark suggested, JSLint gives me an error:

Problem at line 53 character 3: Wrap the entire immediate function invocation in parens.
})(jQuery);

The code looks like:

(function($){
	$(document).ready(function(){
		// Code...
	});
})(jQuery);

Any idea what JSLint is referring to here?

]]>
By: Martin Kirk https://j11y.io/javascript/closures-in-javascript/#comment-1129 Mon, 27 Jul 2009 12:03:18 +0000 https://j11y.io/?p=1017#comment-1129 you might find that using closures will hunt you after some time…

one major problem with event-handling in JS is ‘this’, you never can be 100% sure what ‘this’ is 🙂

ie. you scroll or click on something and want to get the target element – some browsers give you the element by the ‘this’ keyword, others give you the page…

be careful 🙂

]]>
By: Louis https://j11y.io/javascript/closures-in-javascript/#comment-1128 Tue, 21 Jul 2009 14:25:25 +0000 https://j11y.io/?p=1017#comment-1128 Nice article, James.

I just wanted to point out, so as not to confuse beginners, that a closure is not always necessray in the initial example you gave. The closure is only required if you want to access the value of “i” directly. Also, just because “i” is equal to 99 when the object is clicked, does not mean you are clicking on object 99. So if you just need to access the current object, and don’t care about outputting the value of “i”, then you can just use the this keyword instead:

var myElements = [ /* DOM Collection */ ];
 
for (var i = 0; i < 100; ++i) {
    myElements[i].onclick = function() {
        alert( 'You clicked on: ' this.id );
    };
}

So, with the above script, if you had set your elements to have incremental IDs in the HTML, then the alerted value would be equal to the ID of the object clicked. Using this can also be used to access a number of properties of the current object like this.style or this.className, etc. Only if you’re dealing with the variable value itself are you required to use a closure.

Anyhow, thanks for a nice topic of discussion.

]]>
By: zorg https://j11y.io/javascript/closures-in-javascript/#comment-1127 Mon, 20 Jul 2009 13:17:05 +0000 https://j11y.io/?p=1017#comment-1127 @James

> “This behaviour can be used in a number of different ways and has become a useful remedy for quite a few
> JavaScript gotchas; one of the most common being the “looping problem”.

the Javascript gotcha you mention happens the onclick anonymous function closes over a binding to a mutable variable, it is non intuitive but not incorrect! Intuitively, one expects a new binding to a fresh variable at every iteration, that’s what would happen in a language where immutability is the default.

See http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
and http://lambda-the-ultimate.org/node/2648

]]>
By: zorg https://j11y.io/javascript/closures-in-javascript/#comment-1126 Mon, 20 Jul 2009 12:05:57 +0000 https://j11y.io/?p=1017#comment-1126 @Dusan

> I also do use (a lot) javascript and after 15+ years of “real” languages porffesional usage, I came to a
> conclusion that javascript has its appeal *only* inside HTML. It allows for very quick development of
> “awesome stuff”, although not always relevant. And of course browser made this combination realy
> ubiquitous.

Seriously, don’t people ever tire of that “real professional languages” rant ?
Javascript is a cool mix of Scheme and Self (two languages way cooler than most “real” languages.
Javascript is the web lingua franca.
Javascript implementation have upped their games tremendously in the past year, V8 is faster than PHP, CPython, MRI, etc…
Javscript is indeed making inroads outside the browser (http://en.wikipedia.org/wiki/Server-side_JavaScript), coding your whole web app in one language does makes sense, goodbye impedance mismatch

]]>
By: Jani Hartikainen https://j11y.io/javascript/closures-in-javascript/#comment-1125 Sun, 19 Jul 2009 23:34:23 +0000 https://j11y.io/?p=1017#comment-1125 I’m not 100% sure but the looping example with the anonymous function may end up leaking memory. The array has references to the elements, and the closure is going to keep a reference to the array, thus creating a circular reference when you assign the function as a handler in an element.

@Dusan why are you reading this blog if you think it’s pointless? 😛

]]>
By: Bjoern Wibben https://j11y.io/javascript/closures-in-javascript/#comment-1124 Sat, 18 Jul 2009 11:15:38 +0000 https://j11y.io/?p=1017#comment-1124 Hi James,

your blog is great, keep up the good work.

I’m using closures for iteration (simplified example):

var myarr = ["a", "b", "c"];
 
function iterator(arr) {
	var idx = 0;
	return function () {
		return arr[idx++];
	}
}
 
var getNext = iterator(myarr);
 
getNext(); // a
getNext(); // b
getNext(); // c

Best Regards,

Björn

]]>