instanceof 在 JSON.stringify() 内部的行为有何不同?

What does instanceof behave differently inside JSON.stringify()?

我正在使用 decimal.js for some financial calculations in Node. I'm writing a custom JSON.stringify replacer function,但是当我使用 instanceof 测试 属性 类型时,我得到的结果与我在替换函数之外进行相同测试时得到的结果不同.

这是一个可运行的例子:

const myObj = {
    myNum: new Decimal(0.3)
};

// logs 'Property "myNum" is a Decimal: true'
console.log('Property "myNum" is a Decimal:', myObj.myNum instanceof Decimal);

const replacer = (key, value) => {

    if (key === 'myNum') {
        // logs 'Property "myNum" is a Decimal: false'
        console.log('Property "myNum" is a Decimal:', value instanceof Decimal);
    }

    if (value instanceof Decimal) {
        return value.toNumber()
    } else {
        return value;
    }
}

JSON.stringify(myObj, replacer, 4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.0.0/decimal.js"></script>

为什么会这样?

如果我将 Decimal 实例替换为我自己自定义的 class 实例,那么两个 instanceof 测试的行为与预期相同:

function MyClass() {}

const myObj = {
    myClass: new MyClass()
};

// logs 'Property "myClass" is a MyClass: true'
console.log('Property "myClass" is a MyClass:', myObj.myClass instanceof MyClass);

const replacer = (key, value) => {

    if (key === 'myClass') {
        // logs 'Property "myClass" is a MyClass: true'
        console.log('Property "myClass" is a MyClass:', value instanceof MyClass);
    }

    return value;
}

JSON.stringify(myObj, replacer, 4);

想通了。 Decimal 实例包含一个 .toJSON() 方法。当 JSON.stringify 遇到一个定义了 toJSON 函数的对象时,它会调用它并且 returns 结果作为替换函数中的第二个参数而不是对象引用。结果,我上面示例中的 value 变量指向 string,而不是 Decimal 实例。

来自 MDN:

If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior

为了演示这一点,我可以调整上面的第二个示例以包含一个 toJSON 函数:

function MyClass() {

    // add a toJSON method to my custom class
    this.toJSON = () => {
        return 'Hello, world!';
    };
};

const myObj = {
    myClass: new MyClass()
};

// logs 'Property "myClass" is a MyClass: true'
console.log('Property "myClass" is a MyClass:', myObj.myClass instanceof MyClass);

const replacer = (key, value) => {

    if (key === 'myClass') {
        // logs 'Property "myClass" is a MyClass: true'
        console.log('Property "myClass" is a MyClass:', value instanceof MyClass);
    }

    return value;
}

JSON.stringify(myObj, replacer, 4);