如何从definitelyTyped的javascript声明打字稿文件中识别静态方法和实例方法以及其他方法?
比方说,exampleString.length是一个实例方法,而,String.fromCharCode()是一个静态方法。
我一直在寻找一种方法来编写一个可以识别另一个静态方法的脚本。用打字稿服务api说些什么。
答案 0 :(得分:1)
评论太长了。
首先,String.prototype.length
不是方法,而是值。如果使用class
关键字定义构造函数,则可以通过检查方法是在构造函数中还是在实例中定义来检测静态和非静态方法。
class foo{
bar(){}
static baz(){}
}
var instance = new foo();
console.log(instance.constructor.bar); // undefined
console.log(instance.constructor.baz); // [Function: baz]
console.log(instance.bar); // [Function: bar]
console.log(instance.baz); // undefined
因此,您可以得出结论baz
是静态的,bar
是非静态方法。此外,String.length
是个不错的示例:someString.length
引用了String.prototype.length
,但String.length
引用了Function.prototype.length
。