3 ms·
It's not very proper to call these closures, is it? Their purpose is not to close around external variables available in the lexical scope. And I'm ready to up
by subtenante 13y ago
It's not very proper to call these closures, is it? Their purpose is not to close around external variables available in the lexical scope.
And I'm ready to upvote the first blog post entitled "the 'considered harmful' meme considered harmful".
- shmageggy 13y agoThey look like closures to me. In javascript, functions are first class entities, so when he writes function privateFunction() { } that is sugar meaning there is a variable named privateFunction and it points to a function. So the object that is eventually returned closes around these external variables.
- subtenante 13y agoBut if they're really closures, how can we explain such behaviour?: http://jsfiddle.net/9uqZg/ http://jsfiddle.net/9uqZg/
- shmageggy 13y agoBecause you aren't really showing the pattern that is being discussed. In that example, the two incr variables are in the same lexical scope, and you are calling your closure in that same scope. So when your inner function calls incr, it calls the first one it sees, moving outwards through nested scopes. Later, when you overwrite incr, the new one gets called. You've really effectively only closed over the variable x with respect to the lexical scope that incr is in. Here's four variations that hopefully show the difference. http://jsfiddle.net/7ZfvP/1/ http://jsfiddle.net/7ZfvP/1/ edit: bad link
- subtenante 13y agoIndeed, you're right. It still feels quite strange to me to use the fact that a function (used as behaviour and not data) is, from the language features, technically a variable to call that kind of construct a closure. But thanks for the effort and occasion to think about it.