在 Typescript 中访问对象 class 的静态方法?
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()
我不知道 obj 的确切 class,我需要打印 A 或调用 f()
以获得正确的 obj class。
我不需要 class 名称,例如。
我正在做更复杂的事情。
prototype, typeof, constructor
好像都是语法错误
两者 Object.getPrototypeOf() (replacement to the now deprecated Object.prototype.__proto__
) or Object.prototype.constructor
都应该有效:
Object.getPrototypeOf(obj).constructor.f();
obj.constructor.f();
实际上:
Object.getPrototypeOf(obj).constructor === obj.constructor; // true
在这里你可以看到运行中的编译源代码:
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;
}
注意静态属性存在于 类 但不存在于实例中。
因此,如果你想从obj
到它的原型,你应该调用Object.getPrototypeOf(obj)
,而不是obj.prototype
,这是完全不同的事情。
.prototype
属性 仅存在于函数中,当使用 new
实例化新对象并调用该构造函数 (new A()
) 时,将成为新创建的对象的原型(已弃用 .__proto__
)。
在你的例子中:
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
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()
我不知道 obj 的确切 class,我需要打印 A 或调用 f()
以获得正确的 obj class。
我不需要 class 名称,例如。 我正在做更复杂的事情。
prototype, typeof, constructor
好像都是语法错误
两者 Object.getPrototypeOf() (replacement to the now deprecated Object.prototype.__proto__
) or Object.prototype.constructor
都应该有效:
Object.getPrototypeOf(obj).constructor.f();
obj.constructor.f();
实际上:
Object.getPrototypeOf(obj).constructor === obj.constructor; // true
在这里你可以看到运行中的编译源代码:
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;
}
注意静态属性存在于 类 但不存在于实例中。
因此,如果你想从obj
到它的原型,你应该调用Object.getPrototypeOf(obj)
,而不是obj.prototype
,这是完全不同的事情。
.prototype
属性 仅存在于函数中,当使用 new
实例化新对象并调用该构造函数 (new A()
) 时,将成为新创建的对象的原型(已弃用 .__proto__
)。
在你的例子中:
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