Output class property with default value on prototype

Topics: General, Language Specification
Oct 27, 2012 at 6:32 PM
Edited Oct 27, 2012 at 6:39 PM

If i define a property with a default value, why not output this value on  prototype? Ex:

Ex:

class EditPanel extends Ext.Panel {
    title : string = 'foo';
}

output

 

var EditPanel = (function (_super) {
    __extends(EditPanel, _super);
    function EditPanel() {
        _super.apply(this, arguments);

        this.title = 'foo';
    }
    return EditPanel;
})(Ext.Panel);

 

should output

 

var EditPanel = (function (_super) {
    __extends(EditPanel, _super);
    function EditPanel() {
        _super.apply(this, arguments);
    }
    EditPanel.prototype.title = 'foo';
    return EditPanel;
})(Ext.Panel);

Oct 28, 2012 at 2:48 PM

I don't think that would always produce the expected results. For example:

 

function Hello() {
}

Hello.prototype.test = 12;

var h = new Hello();
console.log(h.test); // So far so good

h.test = 13;
console.log(h.test); // Still ok

delete h.test;
console.log(h.test); // Didn't expect this

//Peter

Oct 28, 2012 at 3:54 PM

Interesting, but this behavior is right. When you delete a property value, that property now will have value of prototype. In this case you should assign null value to the property. 

 

We also should assign only primitive values on prototype. If you assign an Array for example, this array will be shared with all objects that inherit the prototype.

Oct 28, 2012 at 4:24 PM

The behavior is indeed correct. But the example was to demonstrate that if you would "under the hood" assign the default value to the prototype, a developer might be confused when he deletes a property, he gets his default value back. 

Also the following might surprise a developer (a typical idiom in JS):

for (var i in h) { 
  if (h.hasOwnProperty(i)) console.log(i);
}

Again, in his mind he just defined a property on a Class, so why he doesn't see it back. Of course you could explain why this behavior is correct, but my feeling is that many developers would have a hard time understanding this different behavior that occurs only when you define a default value.

 

 

 

 

 

Nov 1, 2012 at 2:40 AM

I understand your concern, but personally I use this loop pattern only for Object literals...

I also found a proposal for ES6 indicating that properties should be on prototype when keyword "public" is specified:

 

http://wiki.ecmascript.org/doku.php?id=harmony:classes

 

Member Modifiers

Since a class body defines properties on two objects, syntax is needed to indicate on which object, constructor or prototype, the member becomes a property. Keyword prefixes are used:

class Monster {
  // "static" places the property on the constructor.
  static allMonsters = [];
 
  // "public" declares on the prototype.
  public numAttacks = 0;
 
  // Although "public" is not required for prototype methods, 
  // "static" is required for constructor methods
  static numMonsters() { return Monster.allMonsters.length; }
}

Dec 13, 2012 at 10:49 AM

With a lot of attributes and a lot of instances of the same object it's very memory wasteful not to use prototype for them.

Having it in the prototype, we can have pattern similar to copy-on-write. It's quite common in advanced JS development.

Dec 14, 2012 at 9:55 PM

I personally agree with them NOT being on the prototype by default due to the inherit dangers of array & object properties.  With that said, nothing prevents you from doing the below which will in fact make them prototype properties:

 

class Foo {
	public title: string;
}
Foo.prototype.title = "bar";