Re: "with" statement, extending prototype
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: "with" statement, extending prototype

From: Eric Anderson <eman1000@gmail.com>
Date: Thu Apr 06 2006 - 04:48:16 CEST

Julian Turner wrote:
> In rough terms, when the "with" statement finds an "identifier", e.g.
> "prototype" or "start", within the { ] it tries to find a corresponding
> property of the object supplied to the with statement. But what it
> does not do, is create a new property of the object, if it cannot find
> one.
[...snip example...]
> and there is no way that JavaScript can guess that you
> want to create a new property. For all JavaScript knows, "start" could
> equally be a previously declared variable or a new global variable you
> are wanting to assign a value to.

I took a look at the EMCAScript standard again because this still did
not make sense. Javascript would have to be able to create properties
on whichever object existed at the front of the scope chain. Identifier
resolution is done by searching the scope chain (see section 10.1.4)
and identifiers created in the current execution context would have to
be added to the object at the front so that they are removed when that
execution context ends (i.e. when the object in the front of the scope
chain is removed).

But when looking at the standard I think I did find the answer. I
forgot that Javascript only has three types of execution contexts
(global, eval, and function). When it enters the execution context it
finds all variables declarations and function declarations in that
execution context and assignes them as properties to the variables
object (which is also the activation object, which is the object that
is put on the front of the scope chain when the execution context is
started). See 10.1.3 for more info on this.

This assignment of properties when entering the execution context
includes variables and functions declared in a "with" statement.
Javascript does not create a new execution context when entering the
"with" statement and therefore the functions and variables declared in
the "with" statement are a property of the execution context's
"variable" object.

Based on this understanding the following (in global context):

function Car() {}
with(Car.prototype) {
    function start() {
        alert('starting');
    }
    var foo = 'bar';
}

is equivilant to this (again in global context):

// Behind the scenes
this.Car = function Car() {};
this.start = function start() { alert('starting'); }
this.foo = undefined;

// Executing user code
with( Car.prototype ) {
    foo = 'bar';
}

If the above code executed in a function the identifiers "Car", "start"
and "foo" would be properties of the function's variable object and
therefore once the function returned they would be garbage collected
and not even exist (unless of course a closure referenced that scope
chain therefore causing them to not be garbage collected).

Asssuming my understanding is correct I now understand why my
experiment did not work. Once again I want to say thanks for everyone's
help in making this clear.

Eric
Received on Mon May 1 04:44:28 2006