静态类属性的继承

我是阿斯顿

静态类属性是继承的:

const foo = class { static prop = 'this is a static prop' }
const bar = class extends foo {}
console.log(bar.prop) // 'this is a static prop'

但是我认为静态属性类似于以下内容,它们可以undefined按预期方式打印

const foo = function() {}
foo.prop = 'this is a static prop'
const bar = function() {}
bar.prototype = Object.create(foo.prototype)
bar.prototype.constructor = bar
console.log(bar.prop) // undefined

那么,静态类属性继承的神奇行为是否超出了普通原型继承?

丹尼尔·怀特

是。extends为您做到这一点:MDN您当然可以针对相同的行为进行老式复制/扩展。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章