简体   繁体   English

为什么javascript typeof总是返回“对象”

[英]Why javascript typeof returns always “object”

Where am I doing wrong? 我哪里做错了?

I would wait "Class" as a result of this code but it doesn't: 我会等待“Class”作为此代码的结果,但它不会:

在此输入图像描述

This is from object function: 这是来自对象功能:

在此输入图像描述

Tyepof doesnt work like that, it only returns built in types. Tyepof不会那样工作,它只返回内置类型。 You could try: 你可以尝试:

this.constructor.name==="Class";

It will check all the way up the prototype chain to see if this or any prototype of this is Class. 它会检查所有的方式原型链,看看this或任何原型的this是1类。 So if OtherType.prototype=Object.create(Class); 所以如果OtherType.prototype=Object.create(Class); then it'll be true for any OtherType instances. 那么任何OtherType实例都是如此。 Does NOT work in < IE9 不适用于<IE9

or 要么

this instanceof Class

But that will not check the entire prototype chain. 但这不会检查整个原型链。

Here is a list of return values typeof can return 以下是typeof可返回的返回值列表

Here is an answer about getting the type of a variable that has much more detail and shows many ways it can break. 是一个关于获取具有更多细节的变量类型的答案,并展示了它可以破解的许多方法。

Because JavaScript knows only the following types : 因为JavaScript只知道以下类型:

Undefined - "undefined" 未定义 - “未定义”

Null - "object" 空 - “对象”

Boolean - "boolean" 布尔 - “布尔”

Number - "number" 号码 - “号码”

String - "string" 字符串 - “字符串”

Host object (provided by the JS environment) - Implementation-dependent 主机对象(由JS环境提供) - 依赖于实现

Function object (implements [[Call]] in ECMA-262 terms) - "function" 函数对象(在ECMA-262术语中实现[[Call]]) - “function”

E4X XML object - "xml" E4X XML对象 - “xml”

E4X XMLList object - "xml" E4X XMLList对象 - “xml”

Any other object - "object" 任何其他对象 - “对象”

You can find more here 你可以在这里找到更多

Read this thread to find how you can get the object name 阅读主题以了解如何获取对象名称

object.constructor.name will return constructor's name. object.constructor.name将返回构造函数的名称。 Here is an example: 这是一个例子:

function SomeClass() {
    /* code */
}
var obj = new SomeClass();
// obj.constructor.name == "SomeClass"

Be aware that you need to use named functions, if you assign anonymous functions to variables, it will be an empty string... 请注意,您需要使用命名函数,如果您将匿名函数分配给变量,它将是一个空字符串...

var SomeClass = function () {
    /* code */
};
var obj = new SomeClass();
// obj.constructor.name == ""

But you can use both, then the named function's name will be returned 但是你可以使用两者,然后返回命名函数的名称

var SomeClassCtor = function SomeClass() {
    /* code */
};
var obj = new SomeClassCtor();
// obj.constructor.name == "SomeClass"

You may try this as well 你也可以尝试一下

function getType(obj){
    if (obj === undefined) { return 'undefined'; }
    if (obj === null) { return 'null'; }
    return obj.constructor.name || Object.prototype.toString.call(obj).split(' ').pop().split(']').shift().toLowerCase();
}

An Example Here. 这里的一个例子。

function MyClass(){}
console.log(getType(new MyClass)); // MyClass
console.log(getType([])); // Array
console.log(getType({})); // Object
console.log(getType(new Array)); // Array
console.log(getType(new Object)); // Object
console.log(getType(new Date)); // Date
console.log(getType(new Error));  // Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM