Node.js 如何扩展模块的类?
我正在使用带有Node.js 如何扩展模块的类?,node.js,Node.js,我正在使用带有节点的现代Javascript(EC6+)代码——实验模块 import bigInt from 'big-integer'; // npm i big-integer console.log(bigInt(333).toString(2)) // fine class bigIntB4 extends bigInt { constructor(...args) { super(...args); } test() { co
节点的现代Javascript(EC6+)代码——实验模块
import bigInt from 'big-integer'; // npm i big-integer
console.log(bigInt(333).toString(2)) // fine
class bigIntB4 extends bigInt {
constructor(...args) {
super(...args);
}
test() {
console.log("Hello!")
}
}
let c = new bigIntB4(333) //fine
console.log(c.toString(2)) // fine
c.test() // BUG!
错误:“TypeError:c.test不是一个函数”它是一个返回对象的普通函数。因此,您无法真正扩展它
以下是该问题的简化示例:
函数Foo(){
返回{foo:42};
}
类栏扩展了Foo{
构造函数(){
超级();
}
}
console.log(Bar的新Bar实例);
控制台日志(新条)代码>谢谢菲利克斯!是的,现在我看到了,这是表达在(丑陋的)“旧风格”的模块。。。好吧,正如你所证明的,不重写它是不可能的。说教式的回答。