Re: [JSMentors] Blog post on closures in JavaScript

2011-04-26 Thread Nick Morgan
Thanks! I'd just been watching some SICP videos when I came up with
the idea for the post, so I had that kind of explanation on my mind
(but I wanted to make it accessible).

Nick

On 26 April 2011 19:47, Peter van der Zee jsment...@qfox.nl wrote:
 On Tue, Apr 26, 2011 at 10:48 AM, Nick Morgan skilldr...@gmail.com wrote:

 Hi all

 I've written a new post on my blog, Closures explained with JavaScript.
 http://skilldrick.co.uk/2011/04/closures-explained-with-javascript/

 Mentioning it here for two reasons

 (a) to help the mentees understand this crucial (and often confusing)
 aspect of JS, and
 (b) to ask the mentors if they have any
 feedback/criticism/clarifications...

 Cheers
 --
 Nick Morgan
 http://skilldrick.co.uk
 @skilldrick


 I like the article and have little to say about it otherwise :) The free
 variable approach is something you don't see very often.

 - peter

 --
 To view archived discussions from the original JSMentors Mailman list:
 http://www.mail-archive.com/jsmentors@jsmentors.com/

 To search via a non-Google archive, visit here:
 http://www.mail-archive.com/jsmentors@googlegroups.com/

 To unsubscribe from this group, send email to
 jsmentors+unsubscr...@googlegroups.com




-- 
Nick Morgan
http://skilldrick.co.uk
@skilldrick

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com


Re: [JSMentors] Blog post on closures in JavaScript

2011-04-26 Thread Lasse Reichstein
On Tue, 26 Apr 2011 17:48:45 +0200, Nick Morgan skilldr...@gmail.com  
wrote:



Hi all

I've written a new post on my blog, Closures explained with JavaScript.
http://skilldrick.co.uk/2011/04/closures-explained-with-javascript/

Mentioning it here for two reasons

(a) to help the mentees understand this crucial (and often confusing)
aspect of JS, and
(b) to ask the mentors if they have any  
feedback/criticism/clarifications...


Minor nitpicks only. I like the description.

The ability to nest functions gives us closures.
I'd say that even top-level function expression evaluate to closures. The  
scope they

capture might just be the global scope, but it's still there.
In a language with first-class functions and static scope, all functions  
are (possibly
trivial) closures, where the trivial case is a function with no free  
variables (which

can arguably be said to not be a closure).
As you have said, a closure is code combined with an environment where the  
free variables
are looked up. A top-level function is that, with the global scope as its  
environment.


A variable is free in any particular scope if it is defined within an  
enclosing scope.
I would normally not separate a nested scope from the surrounding scope -  
it's more
like an extension of the outer scope. I would prefer to talk about  
variables being free
in an expression/statement/function declaration, i.e., at the syntactic  
level. However,
it still makes sense as written, so that's just me preferring my  
preference :)


Apart from that, in most other languages, I would agree with the  
statement, because it's
equivalent to being a variable that's not bound by a declaration in the  
function declaration
we are focusing on. Not so in Javascript. In Javascript you can create  
functions with free
variables that aren't defined anywhere (yet), because the scope chain is  
mutable. You can
add global, and even local, variable declarations after you have created  
the function object.


So, I'd say something like:
 A variable is free in a function's code, if it's not bound by a variable  
declaration
 inside that function. Resolving that variable will use the scope where  
the function

 occurs.
or something to that effect, which I think you can make more readable than  
I can.
(Although that still ignores the case where the variable may or may not be  
declared

dynamically inside the function).
It's much harder to describe this in Javascript than in, say, Scheme,  
because variables
in Javascript are resolved every time they are accessed, and the scope  
chain may change

between each access.


Closures are functions that retain a reference to their free variables
Again, in other languages this would be the whole truth. In Javascript,  
the closure

retains a reference to the environment where variables will be looked up.
That might just be a little too tricky for this blog post, though. No need  
to confuse

people more than necessary :)


I really like the summary. It sums up the general concept very well.

/L ' http://fiddle.jshell.net/Gmwuw/ '
--
Lasse Reichstein Holst Nielsen - reichsteinatw...@gmail.com

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com


Re: [JSMentors] Blog post on closures in JavaScript

2011-04-26 Thread Poetro
2011/4/26 Lasse Reichstein reichsteinatw...@gmail.com:
 In Javascript you can create functions with free variables that aren't defined
 anywhere (yet), because the scope chain is mutable.

In JavaScript there is thing called hoisting, so ever variable that is
defined in the function will be defined at the begining of the
function, the position of the definition doesnt really matter. So I
dont really understand what you mean by this.

-- 
Poetro

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com


Re: [JSMentors] Blog post on closures in JavaScript

2011-04-26 Thread Lasse Reichstein

On Tue, 26 Apr 2011 21:31:16 +0200, Poetro poe...@gmail.com wrote:


2011/4/26 Lasse Reichstein reichsteinatw...@gmail.com:
In Javascript you can create functions with free variables that aren't  
defined

anywhere (yet), because the scope chain is mutable.


In JavaScript there is thing called hoisting, so ever variable that is
defined in the function will be defined at the begining of the
function, the position of the definition doesnt really matter. So I
dont really understand what you mean by this.


You can add properties and variables to the scope chain after it has
been created, and remove them again, e.g., by adding and removing
properties on the global object or on with objects.

You can even add and remove variable declarations in function scopes
by using eval.

Example: http://fiddle.jshell.net/Gmwuw/

This shows the same function (geta) referencing different a-variables
over time, as it adds and then removes declarations of a at different
levels of the scope chain.


It's a tricky question, whether a is free in the function:
  function geta(s) {
if (s) eval(s);
return a;
  }
Depending on the passed value of s, a may or may not be bound inside
the function itself.

/L
--
Lasse Reichstein Holst Nielsen - reichsteinatw...@gmail.com

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com