MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.36k stars 480 forks source link

Error for call constructor from Number.prototype.anyMethod, and Keyword 'for' on IE8 #146

Closed zhaohuihua closed 3 years ago

zhaohuihua commented 4 years ago
  1. Symbol.for can't work on the IE8, changed to Symbol['for']
  2. Error for call constructor from Number.prototype.anyMethod
    // Expect to use Decimal.prototype.toFixed replace the original method
    (function() {
    var originalToFixed = Number.prototype.toFixed;
    Number.prototype.toFixed = function(decimal) {
        if (window.Decimal && Decimal.prototype.toFixed) { 
            return new Decimal(this).toFixed(decimal);
        } else {
            return originalToFixed(this, decimal);
        }
    };
    )();

    TestCode (2).toFixed(2); will be run to line 4340 thrown error '[DecimalError] Invalid argument: 2' because line 4290 test typeof v returned 'object' if (t === 'number') changed to if (t === 'number' || v instanceof Number)

MikeMcl commented 4 years ago

I don't see a need for
|| v instanceof Number when new Decimal(this) in your example could simply be replaced with new Decimal(+this) or new Decimal(Number(this)) or new Decimal(this.valueOf()) etc.

MikeMcl commented 3 years ago

I changed Symbol.for to Symbol['for'] in v10.3.0.

Thanks for your input.