javascript - javascript中类型的默认值

标签 javascript types default-value

JavaScript 中是否有一个函数可以返回传递给它的类型名称的默认值?例如:

var defaultValue = built_in_getDefaultValue("number");
console.log(defaultValue); // Logs the number 0 to the console.

最佳答案

之前的两个答案都正确地指出了 JavaScript 中变量的默认(未分配)值是 undefined,但都没有解决您似乎正在寻找的内容——相当于例如C# 中的 default 运算符。

幸运的是,JavaScript 有一个非常简单的类型系统:所有不是七种基本类型之一的东西都是一个对象,可以通过它的构造函数调用。¹下面的函数,给定一个字符串表示

  • JS 原始类型的名称,例如'string''null'
  • typeof 运算符的任何有效产生式,例如'函数';
  • this 值中调用它的函数名称或其局部作用域²

将产生对“默认”值的合理解释:

function defaultVal(type) {
    if (typeof type !== 'string') throw new TypeError('Type must be a string.');
    
    // Handle simple types (primitives and plain function/object)
    switch (type) {
        case 'bigint'    : return BigInt(0);
        case 'boolean'   : return false;
        case 'function'  : return function () {};
        case 'null'      : return null;
        case 'number'    : return 0;
        case 'object'    : return {};
        case 'string'    : return "";
        case 'symbol'    : return Symbol();
        case 'undefined' : return void 0;
    }
    
    try {
        // Look for constructor in this or current scope
        var ctor = typeof this[type] === 'function'
                   ? this[type]
                   : eval(type);
        
        return new ctor;
    
    // Constructor not found, return new object
    } catch (e) { return {}; }
}

你可以这样调用它:

defaultVal( typeof 1 ); // -> 0
defaultVal( 'bigint' ); // -> 0n
defaultVal( 'object' ); // -> {}
defaultVal( 'RegExp' ); // -> /(?:)/

不幸的是,JS typeof 运算符可能有点……对我们的目的来说是一个名字。例如,typeof null === 'object'¹ 和 typeof [] === 'object',这可能不是您想要的。

为了解决这个问题,这里有一个函数返回对值调用 typeof 的结果除非结果是“object”,在这种情况下它将返回 'null' 如果值为 nullobj.constructor.name 否则(以 'object' 作为回退如果一切都失败了的值(value)):

function getType(obj) {
    var type = typeof obj;
    
    if (type !== 'object') return type; // primitive or function
    if (obj === null) return 'null';    // null
    
    // Everything else, check for a constructor
    var ctor = obj.constructor;
    var name = typeof ctor === 'function' && ctor.name;

    return typeof name === 'string' && name.length > 0 ? name : 'object';
}

现在我们可以:

defaultVal( getType( 1234 ) );  // -> 0
defaultVal( getType( [99] ) );  // -> []
defaultVal( getType( 'ab' ) );  // -> ""
defaultVal( getType( null ) );  // -> null   (Not possible with typeof!)
defaultVal( getType( /.*/ ) );  // -> /(?:)/ (Not possible with typeof!)

function T () {}
defaultVal( getType( new T ) ); // -> T {}   (Not possible with typeof!)

我怀疑这与您要寻找的东西一样接近。此外,为了避免任何批评:使用上面的 eval 可能看起来有点脏,但这是从当前范围获取命名值的唯一方法。


¹ null 有点奇怪。 ECMAScript 2020 specification clearly refers to it as a primitive ;但是,由于 a bug in the original JavaScript implementationtypeof null'object' .在某一时刻MDN excluded null from its list of primitives ,仅计算六个基元并声明 null 应被视为“每个 Object 的特例”。

根据规范,我在这个答案中将 null 称为原语,并且 MDN 已经更新以同意这种立场。

² 此函数不能调用在另一个作用域中实例化的构造函数;相反,我已经可以使用绑定(bind)到包含它可能引用的任何此类构造函数的对象的 this 来调用它:

function innerScopeCtor() {
    function T () {}
    
    return defaultVal.call( { T : T }, 'T' );
}

innerScopeCtor(); // T {}

如果您不这样做,它只会退回到返回一个新对象。

关于javascript - javascript中类型的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603106/

相关文章:

javascript - 如何保存更改的 Div(实时)

c - 如何修复 C 中的 "incompatible type"错误?

java - 初始化字段变量

javascript - 如何在 Javascript 中进行 Null 值检查?

javascript - 通过 bool 值验证单选按钮?

methods - 静态类型语言中的多界方法?

css - css中的 "projection"媒体类型有多可靠?

java - 将默认值设置为丰富的:select component which has enableManualInput set to true?

javascript - 如何使用 Jquery 获取添加的输入字段的值?

javascript - 从 JSON 中提取数字时返回 NAN