This project is read-only.

Properties on prototype

Topics: General
Oct 31, 2012 at 7:04 PM
Edited Oct 31, 2012 at 7: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;})();


Oct 31, 2012 at 11:16 PM

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

Nov 1, 2012 at 10: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 4:00 PM
Edited Nov 1, 2012 at 4: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.