Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining prototype property in TypeScript

Tags:

typescript

I have a class, let's say A. I need to declare there a prototype property that can be accessible as follows:

var idKey = A.prototype.attributeId;

I can do it using the following code:

class A {
  constructor() {
      A.prototype.attributeId = "InternalId";
  }
}

Is there a better way of doing it?

like image 250
ie. Avatar asked Nov 06 '14 13:11

ie.


People also ask

What is a property prototype?

The prototype property is an object which contains a constructor property and its value is Point2D function: Point2D.prototype.constructor = Point2D . And when you call Point2D with new keyword, newly created objects will inherit all properties from Point2D.prototype .

What is difference between __ proto __ and prototype?

prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.

Has prototype property JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.


1 Answers

This is not ideal, but it suits your needs.

class A {
    attributeId:string;
}
A.prototype.attributeId = "InternalId";

This gets compiles to es5 as:

var A = (function () {
    function A() {
    }
    return A;
})();
A.prototype.attributeId = "InternalId";
like image 152
Martin Avatar answered Sep 29 '22 02:09

Martin