"xxx : this.yyy" in prototype

# Aymeric Vitte (13 years ago)

I did not find any related subject in discussions or specs so here it is :

var f=function(name) { this._name=name; };

f.prototype={ get nodeName() {return this._name}, get tagName() {return this._name}, get anotherNameInvention() {return this._name}, ... set nodeName() {}, set tagName() {}, set anotherNameInvention() {}, ... };

So in that case we have to define several getters and setters for something that is refering to the same object, slowing down the overall script execution, it would probably be better to have something like :

f.prototype={ nodeName <operator> this._name, tagName <operator> this._name, anotherNameInvention <operator> this._name ... }

var g=new f('test') --> g.nodeName is equal to g._name

While calling new the <operator> would indicate that g.nodeName must be

equal to g._name whether _name is a property or a method.

nodeName, tagName, etc could be defined in f but this does not solve the case if we want them to be inherited properties.

Maybe no <operator> is required and the default behavior should be the

one described above (what could be the use of declaring something like xxx : this.yyy in prototype ??? I have never seen it).

Example of use : js w3c dom implementation where objects own a lot of redundant properties.

A. Vitte

# Aymeric Vitte (13 years ago)

Declaring something like a:this.b in prototype is of no use (because in all cases it will be equivalent at the end to declare something like a:c where c=("this" bounded object).b).

Intuitively, if we forget the specs and current behaviour, we could think that doing such, "this" could have a chance to refer to the new created object after the "new" statement, but of course it is not the case.

Instead we have to define getters and setters as mentionned below.

This can be compared to the var that/self=this trick in terms of repeatability.

Something that is useless could be transformed to something usefull, but right now I don't see a simple way to do it that creates more benefit than harm.

A. Vitte

Le 15/02/2012 19:21, Aymeric Vitte a écrit :