简体   繁体   English

在 Typescript 中访问对象的类的 static 方法?

[英]Access a object's class's static methods in Typescript?

class Base {
  static f(){console.log('Base')}
}

class A extends Base {
  static f(){console.log('A')}
}

class B extends Base {
  static f(){console.log('B')}
}

let obj: A|B = new A()

obj.<what to put here>.f()

I don't know the exact class for obj, I need to print A or just call f() for correct class of obj.我不知道 obj 的确切 class,我需要打印 A 或只调用f()以获得正确的 obj 的 class。

I don't need just the class name, that is for example.例如,我不需要 class 名称。 I am doing more complex things.我正在做更复杂的事情。

prototype, typeof, constructor all seem to be syntax errors. prototype, typeof, constructor似乎都是语法错误。

Both Object.getPrototypeOf() (replacement to the now deprecated Object.prototype.__proto__ ) or Object.prototype.constructor should work: Object.getPrototypeOf() (替换为现已弃用的Object.prototype.__proto__ )或Object.prototype.constructor .

Object.getPrototypeOf(obj).constructor.f();

obj.constructor.f();

Actually:实际上:

Object.getPrototypeOf(obj).constructor === obj.constructor; // true

Here you can see the compiled source in action:在这里,您可以看到编译后的源代码:

 class Base { static f() { console.log('Base'); } } class A extends Base { static f() { console.log('A'); } } class B extends Base { static f() { console.log('B'); } } const objBase = new Base(); const objA = new A(); const objB = new B(); Object.getPrototypeOf(objBase).constructor.f(); objBase.constructor.f(); Object.getPrototypeOf(objA).constructor.f(); objA.constructor.f(); Object.getPrototypeOf(objB).constructor.f(); objB.constructor.f(); console.log(Object.getPrototypeOf(objB).constructor === objB.constructor); console.log(Object.getPrototypeOf(objB) === B.prototype);
 .as-console-wrapper { max-height: none;important; }

Note static properties exist in classes but not in instances.注意 static 属性存在于类中,但不存在于实例中。

Therefore, if you want to go from obj to its prototype, you should call Object.getPrototypeOf(obj) , not obj.prototype , which is a completely different thing.因此,如果你想 go 从obj到它的原型,你应该调用Object.getPrototypeOf(obj) ,而不是obj.prototype ,这是完全不同的事情。

The .prototype property only exists in functions and, when instantiating a new object using new and a call to that constructor function ( new A() ), will become the newly created object's prototype (the deprecated .__proto__ ). .prototype属性仅存在于函数中,并且当使用new实例化新的 object 并调用该构造函数 function ( new A() ) 时,将成为新创建的对象的原型(已弃用的.__proto__ )。

In your example:在您的示例中:

obj.prototype;                // undefined
A;                            // class A { static f() { ... } }
A.protoype;                   // { constructor: class A { ... } }
A.protoype.constructor;       // class A { static f() { ... } }
A.protoype.constructor === A; // true
obj.constructor;              // class A { static f() { ... } }
obj.constructor === A;        // true

Object.getPrototypeOf(obj) === A.prototype; // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM