1

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

typeof null

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

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

4

3 回答 3

3

ECMAScript 5 对象有一个称为[[Class]]. 这是 ES5 中最接近您所要求的内容。您可以通过[[Class]]以下方式访问Object.prototype.toString

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); // => "???"
于 2012-11-01T21:23:53.867 回答
1

该行为typeof由规范,第 11.4.3 节,表 20 规定:

表 20 — typeof 运算符结果

Type of val                      Result
-----------------------------------------------------------------------------------
Undefined                        "undefined"
-----------------------------------------------------------------------------------
Null                             "object"
-----------------------------------------------------------------------------------
Boolean                          "boolean"
-----------------------------------------------------------------------------------
Number                           "number"
-----------------------------------------------------------------------------------
String                           "string"
-----------------------------------------------------------------------------------
Object (native and does not 
implement [[Call]])              "object"
-----------------------------------------------------------------------------------
Object (native or host 
and does implement [[Call]])     "function"
-----------------------------------------------------------------------------------
Object (host and does not        Implementation-defined except may not be 
implement [[Call]])              "undefined", "boolean",  "number", or "string".

(CapitalizedNull是一种内部类型,其唯一值是null。)

这似乎只是一个约定;null instanceof Objectis false,所以,正如所料,null显然不是一个对象。

您要求的操作员不存在。要找出名称,您可以使用=== null来测试nulltypeof其他原语。至于“自定义伪经典对象”,您可以使用的工具有:

(从this question列出,您可以在其中看到一些示例。)

于 2012-11-01T03:32:16.510 回答
0

您现在可以在所有浏览器中执行此操作。

任何对象的constructor属性都将返回拥有该对象原型的函数。

function Person() { }
alert(new Person().constructor === Person) //true
于 2012-11-01T02:41:16.943 回答