javascript - ECMA-5 是否存在新类型的运算符(或函数)?

标签 javascript ecmascript-5

我认为 JavaScript 有 5 种基本类型(null、undefined、boolean、number、string),然后是对象(包括数组、函数和自定义伪类对象)。但有点奇怪的是

typeof null

"object" ,并且没有简单的方法来取回伪经典类的对象类名,例如 Person , Author .我想知道是否有更新的运算符或函数可以为原始类型返回可能的小写名称(对于 "null" ,而不是 null 返回 "object"),并为自定义伪经典对象返回大写字母?

如果 ECMA-5 或更高版本中不存在这样的运算符或函数,拥有它是否有意义?否则,我们可能需要依赖我们自己的定义或任何框架,但这将不是跨平台的标准。

最佳答案

ECMAScript 5 对象有一个称为 [[Class]] 的内部属性。这是 ES5 中最接近您要求的内容。您可以使用 Object.prototype.toString 访问 [[Class]],如下所示:

function getClassOf(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1);
}

getClassOf([ ]); // => "Array"
getClassOf(new Date()); // => "Date"
getClassOf(function() { }); // => "Function"
getClassOf(3); // => "Number"
getClassOf(true) // => "Boolean"
getClassOf(document.createElement('div')); // => "HTMLDivElement"
getClassOf(Math); // => "Math"
getClassOf(null); // => "Null"
getClassOf(undefined); // => "Undefined"
getClassOf({ x: 1 }); // => "Object"

此行为对于充分识别来自其​​他帧的对象至关重要。

但是,它不适用于用户定义的构造函数。使用用户定义的构造函数创建的对象具有 [[Class]] "Object"

function Foo() { }
var foo = new Foo();

getClassOf(foo); // => "Object"

看起来 ECMAScript 6 可能具有扩展 Object.prototype.toString 返回的内容的能力,因此 getClassOf(foo) 可以是 "Foo " 通过 @@toStringTag 符号。

参见 https://mail.mozilla.org/pipermail/es-discuss/2012-September/025344.html有关即将发布的标准的更多信息。


您可以创建自己的函数来执行您想要的操作:

function getTypeOf(value) {

    // Return "null" for null.
    if (value === null) return 'null';

    // Return primitive types.
    var type = typeof value;
    if (type != 'object') return type;

    // Return [[Class]] if available for objects.
    type = Object.prototype.toString.call(value).slice(8, -1);
    if (type != 'Object') return type;

    // Return "Object" if it wasn't created with another constructor.
    var proto = Object.getPrototypeOf(value);
    if (proto == Object.prototype)
        return 'Object';

    // Return the constructor name if constructor hasn't been
    // modified on the object.
    if (value.constructor && proto === value.constructor.prototype)
        return value.constructor.name;

    // Return the constructor name if constructor hasn't been
    // modified on the prototype.
    if (proto.constructor && proto === proto.constructor.prototype)
        return proto.constructor.name;

    // Return "???" if the type is indeterminable.
    return '???';

}

例子:

getTypeOf([ ]); // => "Array"
getTypeOf(new Date()); // => "Date"
getTypeOf(function() { }); // => "Function"
getTypeOf(3); // => "number"
getTypeOf(true) // => "boolean"
getTypeOf(document.createElement('div')); // => "HTMLDivElement"
getTypeOf(Math); // => "Math"
getTypeOf(null); // => "null"
getTypeOf(undefined); // => "undefined"
getTypeOf({ x: 1 }); // => "Object"

function Foo() { }
var foo = new Foo();

getTypeOf(foo); // => "Foo"

// If the constructor property is changed, still works.
foo.constructor = function FakeConstructor() { };
getTypeOf(foo); // => "Foo"

// If the constructor property AND the prototype's constructor is
// changed, result is "???".
foo.constructor = function FakeConstructor() { };
Foo.prototype.constructor = function FakeConstructor2() { };
getTypeOf(foo); // => "???"

关于javascript - ECMA-5 是否存在新类型的运算符(或函数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170191/

相关文章:

javascript - 在没有 Object.defineProperty 的情况下定义不可写属性?

javascript - 将 block 与对象初始值设定项区分开来

Javascript AJAX 调用, 'GET' URL 中的多个值

javascript - 如何检测页面高度何时发生变化?

javascript - Cesium实时使用本地时间而不是UTC

javascript - 为什么新的慢?

javascript - 有没有一种方法可以用一行 JavaScript 有条件地调用函数?

javascript - 等效于 ES6 到 ES5 中的集合

javascript - date.toLocaleString 有错误吗?

javascript - 切换导航栏中的文本发光?