需要帮助了解原型如何在 JavaScript 中传播

问题描述 投票:0回答:1

在下面的代码中,我尝试轻松检查给定变量的“typeof”,假设它已经声明了。当 Array 作为“对象”返回时,本机 typeof 检查不太令人满意。我的代码大部分按预期工作: '这是一个字符串'.typeof() 产生 'String';和 Object.prototype.typeof('这是一个字符串') 产生 'String';但是当我尝试 NaN.typeof() 时,它产生“Number”,但 Object.prototype.typeof(NaN) 按预期产生“NaN”。这种差异不太可能对我将来有用,但我只是想了解为什么会出现不同的结果。非常感谢您的见解。谢谢。

if (!Object.prototype.hasOwnProperty('stringify')) {
    Object.defineProperties(Object.prototype, {
        stringify: {
            value: function () { 
                if (arguments.length >= 2) return JSON.stringify(this, ...arguments); //this means replacer is used
                return JSON.stringify(this, null, ...arguments) } //when replacer ( function, array) is skipped.
        },
        getType: { value: function(target) {
                if (arguments.length === 0) target = this; //assuming no error, variable.typeof() will yield the target. To avoid error use Object.prototype.typeof(target);
                if (typeof target === 'number' && Number.isNaN(target)) return 'NaN';//NaN.typeof() will return "number" even if it's not a number
                if (typeof target !== 'object') return typeof target; //when typeof returns a primitive (Function, String, Number, BigInt, Symbol, Boolean, undefined); 
                const return_value = Object.prototype.toString.call(target);
                // we can also use regex to do this...
                const type = return_value.substring(
                        return_value.indexOf(" ") + 1, 
                        return_value.indexOf("]"));
                return type.toLowerCase();
            }
        },
        typeof: {value: function(target) { //this should be same as getType() above. weird that NaN.typeof() still  yields 'Number' but Object.prototype.typeof(NaN) yields 'NaN'
            if (arguments.length === 0) target = this; //in case target is of 'null' value;
            return target === undefined ? 'undefined' : target === null ? 'null' : Object.is(NaN, target)? 'NaN' : target.constructor.name;
        }},
        isTypeof: {value: function(type, val) {
            return ([undefined, null].includes(val) && val === type) || Object.is(NaN, val) || val.constructor.name === type;
        }},
        is: {value: function() {
            if (arguments.length === 1) {
                let target = this;
                return Object.prototype.isTypeof(arguments[0], target);
            } else if (arguments.length === 2) {
                return Object.prototype.isTypeof(...arguments);
            }
        }}
    });
}

console.log(Object.prototype.typeof(NaN)) //gets 'NaN' as expected.
console.log(NaN.typeof()); //expecting 'NaN' not 'Number'

参见上面的解释。

javascript typeof
1个回答
0
投票

发生这种情况是因为您在草率模式而不是严格模式下运行脚本。在草率模式下,

this
始终会转换为对象,而在严格模式下,它可以是原始值,而无需包装到对象中。

因此,只需将

"use strict"
添加为每个需要
this
作为相关原语的函数内的第一行,或者添加到脚本顶部的全局级别。

例如:

        typeof: {value: function(target) { 
            "use strict"
            if (arguments.length === 0) target = this;
            return target === undefined ? 'undefined' 
                 : target === null ? 'null' 
                 : Object.is(NaN, target)? 'NaN' 
                 : target.constructor.name;
        }},     
© www.soinside.com 2019 - 2024. All rights reserved.