首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ECMA-5是否存在新类型的运算符(或函数)?

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

Stack Overflow用户
提问于 2012-11-01 10:38:38
回答 3查看 256关注 0票数 1

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

代码语言:javascript
复制
typeof null

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-02 05:23:54

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

代码语言:javascript
复制
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"

代码语言:javascript
复制
function Foo() { }
var foo = new Foo();

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

看起来ECMAScript 6可能有能力扩展Object.prototype.toString返回的内容,这样getClassOf(foo)就可以通过@@toStringTag符号进行"Foo"了。

有关即将推出的标准的更多信息,请参阅https://mail.mozilla.org/pipermail/es-discuss/2012-September/025344.html

您可以创建自己的函数来执行您想要的操作,如下所示:

代码语言:javascript
复制
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 '???';

}

示例:

代码语言:javascript
复制
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); // => "???"
票数 3
EN

Stack Overflow用户

发布于 2012-11-01 11:32:17

表20中的规范section 11.4.3规定了typeof的行为:

表20 - typeof Operator Results

代码语言:javascript
复制
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".

(大写的Null是唯一值为null的内部类型。)

这似乎只是一个约定;null instanceof Objectfalse,因此,正如预期的那样,null显然不是一个对象。

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

(列表来自this question,您可以在其中看到一些示例。)

票数 1
EN

Stack Overflow用户

发布于 2012-11-01 10:41:17

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

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

代码语言:javascript
复制
function Person() { }
alert(new Person().constructor === Person) //true
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13170191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档