有没有办法用JSDoc记录JS中的最后一个方法。
Java中的最后一个方法是不能被覆盖的方法。
我在JSDoc网站上找不到任何选择。
发布于 2021-09-01 13:22:16
看来你做不到,因为a search of the JSDoc documentaton for final没有结果。
这并不令人惊讶,因为您无法可靠地在JavaScript中找到最终的方法。因此,如果方法不被覆盖是很重要的,那么您可能不得不在它的描述中记录这一点。尽管如此,JSDoc还是有可能提供一种注释的方法,对于工具来说,它似乎没有。
下面是一种不可靠的方法,可以在JavaScript中获得最后一个方法:
class Base {
constructor() {
if (this.finalMethod !== Base.prototype.finalMethod) {
throw new Error(`You must not override 'finalMethod' in your subclass.`);
}
}
finalMethod() {
console.log("This is the pseudo-final method");
}
}
class Derived extends Base {
finalMethod() {
console.log("This is the overridden method");
}
}
const d = new Derived(); // Throws error
但这在几个方面都很容易克服:
直到子类构造函数调用了超类constructor.
return Object.assign(Object.create(this), { finalMethod() { /*...*/ } }); (尽管我现在想到它,但这实际上是一种不调用超类构造函数的proxy.
https://stackoverflow.com/questions/69014519
复制相似问题