peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

How to extends BigInter? #170

Closed ppKrauss closed 5 years ago

ppKrauss commented 5 years ago

See https://stackoverflow.com/q/54639345/287948

Yaffle commented 5 years ago

At least, you can modify the prototype:

bigInt.prototype.test = function () {
  console.log("Hello!");
};
ppKrauss commented 5 years ago

Thanks @Yaffle , yes. Well... There are plans to rewrite all BigInter module for ES6, as lightweight package, a class for "only complementar methods to native BigInt"?

peterolson commented 5 years ago

You could use bigInt(1).constructor to get the constructor function and extend that, but I don't recommend it because behind the scenes there are three different constructors: BigInteger, SmallInteger, and NativeBigInt, and the library chooses between them based on the size of the number and the environment's support for native big integers.

I don't know what your use-case is, but I can't think of any benefit of extending BigInt that you couldn't do with a simple wrapper.

There are no plans to rewrite BigInteger in ES6+ - I still plan to maintain backwards compatibility with ES5 environments.