TypeScript and __proto__ attribute

Therefore, every time __proto__ is mentioned, it is usually mentioned that Brendan Eich does not use it. I have been using some reflections in Typescript to navigate the prototype chain of the class To use its provided ancestor class, and be willing to inject a prototype attribute that holds class metadata.

Does anyone have any details about the performance overhead that I may incur, or have a problem? Solutions that rely on __proto__?

Edit-update the code. This is just a contrived example I entered, but it illustrates what I hope to do. I am not sure how to benchmark the deceleration of the experience caused by the __proto__ mutation Test. But anyway, I tried it. There is no difference between instantiation, prototype property access and method call execution when modified.

class Base {
public getClassName (): string {
return this['_className'] || undefined;
}
}

class Intermediate extends Base {
}

class Final extends Intermediate {
}

function traverseProtoChain(derivedClass, baseClass) {
var cursor = derivedClass.prototype;
while (cursor instanceof baseClass) {
if (isDefined(cursor.constructor)) {
var className = getProtoName(cursor);
if (isValidString(className))
cursor['_className' ] = getProtoName(cursor);
}

if (isDefined(cursor.__proto__)) {
cursor = cursor.__proto__;
}
}
}

You can use ECMAScrip t 5.1 standard:

Object.getPrototypeOf(cursor)

For very old browsers, if Object.getPrototypeOf does not exist, you You can try to fall back to __proto__, but based on your specific context, you can decide whether these browsers are important.

This is an example that shows this. bar.prototype does not work because it is a Example. The working principle and answer of getPrototypeOf are the same as that of discouraging __proto__.

class Foo {
constructor(name: string) {

}
}

class Bar extends Foo {

}

var bar = new Bar('x');

console.log(bar.prototype);
console.log(Object.getPrototypeOf(bar));
console.log(bar.__proto__);

So you can write “make everyone happy”……

if (Object.getPrototypeOf) {
console.log(Object.getPrototypeOf(bar)) ;
} else if (bar.__proto__) {
console.log(bar.__proto__);
}

The final curve ball __proto__ is likely to be in ECMAScript 6 became standardized… worth remembering!

Therefore, every time __proto__ is mentioned, it is usually mentioned that Brendan Eich does not use it. I have been using some reflections in Typescript to convert the class The prototype chain navigates to the ancestor class that uses it, and is willing to inject a prototype attribute that holds class metadata.

Does anyone have any details on the performance overhead that I may incur, Or is there a solution that does not rely on __proto__?

Edit-update the code. This is just a contrived example I entered, but it illustrates what I hope to do. I am not sure how to benchmark the deceleration of the experience caused by the __proto__ mutation Test. But anyway, I tried it. There is no difference between instantiation, prototype property access and method call execution when modified.

class Base {
public getClassName (): string {
return this['_className'] || undefined;
}
}

class Intermediate extends Base {
}

class Final extends Intermediate {
}

function traverseProtoChain(derivedClass, baseClass) {
var cursor = derivedClass.prototype;
while (cursor instanceof baseClass) {
if (isDefined(cursor.constructor)) {
var className = getProtoName(cursor);
if (isValidString(className))
cursor['_className' ] = getProtoName(cursor);
}

if (isDefined(cursor.__proto__)) {
cursor = cursor.__proto__;
}
}
}

You can use the ECMAScript 5.1 standard:

Object.getPrototypeOf (c ursor)

For very old browsers, if Object.getPrototypeOf does not exist, you can try to fall back to __proto__, but based on your specific context, you can decide whether these browsers are important. /p>

This is an example that shows this. bar.prototype does not work because it is an instance. The working principle and answer of getPrototypeOf are the same as discouraging __proto__.

class Foo {
constructor(name: string) {

}
}

class Bar extends Foo {
< br />)

var bar = new Bar('x');

console.log(bar.prototype);
console.log(Object. getPrototypeOf(bar));
console.log(bar.__proto__);

So you can write “make everyone happy”……

if (Object.getPrototypeOf) {
console.log(Object.getPrototypeOf(bar));
} else if (bar.__proto__) {
console.log(bar.__proto__ );
}

The final curveball __proto__ is likely to become standardized in ECMAScript 6… worth remembering!

Leave a Comment

Your email address will not be published.