Properties on prototype

Topics: General
Oct 31, 2012 at 6:04 PM
Edited Oct 31, 2012 at 6:11 PM

Hi, is there an equivalent to the following JavaScript in TypeScript?

 

function Hater() {

}

Hater.prototype.commonProps = { aaa: "AAA" };

If we try 

class Hater(){

 public commonProps = { aaa: "AAA" };

}
This compiles into an instance property:
var Hater = (function () {   

function Hater() {       

   this.commonProps = {aaa: "AAA" };
}   
return Hater;})();

Thanks.

Oct 31, 2012 at 10:16 PM

I asked a similar question, but this behavior is by design.

http://typescript.codeplex.com/discussions/400987

Nov 1, 2012 at 9:27 AM

Hi, Fabioparra,

I think your suggestion was: "when a property is declared with a default value then it should be defined on the prototype".

And the answer was "if you would "under the hood" assign the default value to the prototype, a developer might be confused".

The question above is slightly different:

How do you explicitly define a property on the prototype?

Nov 1, 2012 at 3:00 PM
Edited Nov 1, 2012 at 3:02 PM

Yes, they answered that it may confuse developers. But according ES6 proposal if you declare a property as "public" it should be on prototype, and only if you declare as static it should be placed on constructor. So in your example property "commonProps" should be placed in prototype. Its how works for frameworks like ExtJS.

 

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