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?
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 .
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.
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.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With