检查值是否为可 new 调用的构造函数
new
箭头函数没有 prototype,返回 false;class 和普通 function 返回 true。 支持泛型:isConstructor<MyClass>(val) 通过后将 val 收窄为 Constructor<MyClass>。
prototype
false
true
isConstructor<MyClass>(val)
Constructor<MyClass>
— 待检查的任意值
true 当且仅当 val 是可构造的函数(有 prototype),同时收窄为 Constructor<T>
Constructor<T>
isConstructor(class Foo {}); // true → val: Constructor<object>isConstructor<Foo>(class Foo {}); // true → val: Constructor<Foo>isConstructor(() => {}); // falseisConstructor(null); // false Copy
isConstructor(class Foo {}); // true → val: Constructor<object>isConstructor<Foo>(class Foo {}); // true → val: Constructor<Foo>isConstructor(() => {}); // falseisConstructor(null); // false
检查值是否为可
new调用的构造函数箭头函数没有
prototype,返回false;class 和普通 function 返回true。 支持泛型:isConstructor<MyClass>(val)通过后将 val 收窄为Constructor<MyClass>。