最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何扩展一个类模块的?

IT培训 admin 2浏览 0评论

如何扩展一个类模块的?

我使用的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);

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论