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

Reply via email to