假设我们有:
class FinalClass {
...
}
如何修改它以使
class WrongClass extends FinalClass {
...
}
或者
new WrongClass(...)
生成异常?也许最明显的解决方案是在 FinalClass 的构造函数中执行以下操作:
if (this.constructor !== FinalClass) {
throw new Error('Subclassing is not allowed');
}
有没有人有更清洁的解决方案,而不是在每个应该是最终的类中重复这些行(可能使用装饰器)?
最佳答案
检查this.constructor
在 FinalClass
的构造函数中如果它不是它自己,则抛出。 (借用检查 this.constructor
而不是 this.constructor.name
来自@Patrick Roberts。)
class FinalClass {
constructor () {
if (this.constructor !== FinalClass) {
throw new Error('Subclassing is not allowed')
}
console.log('Hooray!')
}
}
class WrongClass extends FinalClass {}
new FinalClass() //=> Hooray!
new WrongClass() //=> Uncaught Error: Subclassing is not allowed
或者,在支持下,使用
new.target
.谢谢@loganfsmyth。class FinalClass {
constructor () {
if (new.target !== FinalClass) {
throw new Error('Subclassing is not allowed')
}
console.log('Hooray!')
}
}
class WrongClass extends FinalClass {}
new FinalClass() //=> Hooray!
new WrongClass() //=> Uncaught Error: Subclassing is not allowed
______
正如您所说,您也可以使用装饰器来实现此行为。
function final () {
return (target) => class {
constructor () {
if (this.constructor !== target) {
throw new Error('Subclassing is not allowed')
}
}
}
}
const Final = final(class A {})()
class B extends Final {}
new B() //=> Uncaught Error: Subclassing is not allowed
正如 Patrick Roberts 在评论中分享的装饰器语法
@final
仍在提案中。它适用于 Babel 和 babel-plugin-transform-decorators-legacy .
关于javascript - 如何使 ES6 类最终(不可子类化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38748325/