On 07.09.2011 15:21, Dmitry Pashkevich wrote:
On Wednesday, September 7, 2011 2:57:17 PM UTC+4, Dmitry A. Soshnikov wrote:

    And this variable is not read-only, you may assign to it
    (*while cannot to function's name*).

But I can do this:
function x() {};
x = 123;
x; // 123


Right, because in this case you create a FD (function declaration) which name "x" becomes the name of a binding in the environment -- without read-only attribute (though, it's non-configurable, as well as other vars -- that is, you can't delete them). Schematically:

TheGlobalEnvironment = {

  x: <reference to function "x">,

  // other bindings incl. built-ins

};

And because JS is a dynamic and mutable language, you may of course re-assign the "x" binding to new value:

x = 123; // TheGlobalEnvironment.x = 123

But in case of a NFE (named function expression) a _special object_ object is created which stores the binding of the function's name. And this binding is _immutable_. Thus, this special object is inserted _before_ the environment, thus just _shadowing_ the name of a binding with the same name in the environment:

Let's say you have:

var x = function x() {
  x = 123;
  alert(x);
};

then:

1. Global "x" binding is created and attached to the result of (3)
2. Special object is created with a single property "x" -- the name of a function 3. A function is created which gets its scope chain as SpecialObject + TheGlobalEnvironment: {x: function, ReadOnly: true} -> {x: function, ReadOnly: false}

and then, when you refer "x" from the body of the function, of course the first "x" is found -- that is in this specially created object to store the reference to the function -- only for the time a function is executed.

Dmitry.

I just can't understand the environment that takes place when the code inside the function executes (in the original post)
!function x(){*x=123;alert(x);*}()
--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to