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: Julian Turner <julian@baconbutty.com>
Date: Wed Apr 05 2006 - 16:23:26 CEST

eman1000@gmail.com wrote:

[snip]
> I'm not sure I understand why:
>
> with (Car) {
> prototype.start = function() {alert('starting');};
> }
>
> isn't equivilant to:
>
> with (Car.prototype) {
> start = function() {alert('starting');};
> }
[/snip]

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.

So:-

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

works, because "prototype" is an existing property of Car.

So it becomes the equivalent of "Car.prototype.start=", which is a
JavaScript valid way of creating a new property named "start".

and

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

does not work, because "start" is not an existing property of
"prototype", 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.

The effect of the above code is to create a new "global" variable
called "start", and Car.prototype remains unchanged.

Regards

Julian Turner
Received on Mon May 1 04:42:52 2006