如何扩展一个类模块的?
我使用的JavaScript现代(EC6 +)代码node --experimental-modules
。
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!
错误:“类型错误:C.TEST是不是一个函数”
回答如下:bigInt
is not a constructor function.这是一个正常的函数返回一个对象。因此你真的不能扩展。
这是问题的一个简单的例子:
function Foo() {
return {foo: 42};
}
class Bar extends Foo {
constructor() {
super();
}
}
console.log(new Bar instanceof Bar);
console.log(new Bar);
如何扩展一个类模块的?
我使用的JavaScript现代(EC6 +)代码node --experimental-modules
。
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!
错误:“类型错误:C.TEST是不是一个函数”
回答如下:bigInt
is not a constructor function.这是一个正常的函数返回一个对象。因此你真的不能扩展。
这是问题的一个简单的例子:
function Foo() {
return {foo: 42};
}
class Bar extends Foo {
constructor() {
super();
}
}
console.log(new Bar instanceof Bar);
console.log(new Bar);