如何在 Javascript 中检查实例的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12018759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the class of an instance in Javascript?
提问by mko
Possible Duplicate:
How to get a JavaScript Object's Class?
可能的重复:
如何获取 JavaScript 对象的类?
In Ruby I could do this to check the class of an instance:
在 Ruby 中,我可以这样做来检查实例的类:
'this is an string'.class
=>String
Is there any similar thing in js?
js中有没有类似的东西?
回答by Nathan Wall
You probably mean type or constructor, not class. Class has a different meaning in JavaScript.
您可能指的是类型或构造函数,而不是类。类在 JavaScript 中有不同的含义。
To get the class:
获取课程:
var getClassOf = Function.prototype.call.bind(Object.prototype.toString);
getClassOf(new Date()); // => "[object Date]"
getClassOf('test string'); // => "[object String]"
getClassOf({ x: 1 }); // => "[object Object]"
getClassOf(function() { }); // => "[object Function]"
To get the constructor or prototype there are several ways, depending on what you need.
要获取构造函数或原型,有多种方法,具体取决于您的需要。
To discover what type of primitive you have, use typeof
. This is the best thing to use with strings, booleans, numbers, etc:
要发现您拥有哪种类型的原语,请使用typeof
. 这是与字符串、布尔值、数字等一起使用的最佳方法:
typeof 'test string'; // => 'string'
typeof 3; // => 'number'
typeof false; // => 'boolean'
typeof function() { }; // => 'function'
typeof { x: 1 }; // => 'object'
typeof undefined; // => 'undefined'
Just beware that null
acts weird in this case, as typeof null
will give you "object"
, not "null"
.
请注意,null
在这种情况下,行为很奇怪,因为typeof null
会给您"object"
,而不是"null"
.
You can also get the prototype, the backbone JavaScript inheritance, with Object.getPrototypeOf(myObject)
(or myObject.__proto__
or myObject.constructor.prototype
in some browsers when getPrototypeOf
isn't supported).
您还可以使用Object.getPrototypeOf(myObject)
(myObject.__proto__
或myObject.constructor.prototype
在getPrototypeOf
不支持的某些浏览器中)获取原型,即主干 JavaScript 继承。
You can test the constructor with instanceof
:
您可以使用以下方法测试构造函数instanceof
:
new Date() instanceof Date; // => true
You can also reasonably get the constructor with myObject.constructor
, although be aware that this can be changed. To get the name of the constructor, use myObject.constructor.name
.
您也可以使用 合理地获取构造函数myObject.constructor
,但请注意这可以更改。要获取构造函数的名称,请使用myObject.constructor.name
.
new Date().constructor.name; // => 'Date'
回答by KooiInc
Not sure if this goes for all browsers, but you can use constructor.name
:
不确定这是否适用于所有浏览器,但您可以使用constructor.name
:
'some string'.constructor.name; //=>String
({}).constructor.name //=>Object
(7.3).constructor.name //=>Number
[].constructor.name //=>Array
(function(){}).constructor.name //=>Function
true.constructor.name //=>Boolean
/test/i.constructor.name //=>RegExp
(new Date).constructor.name //=>Date
(new function MyConstructor(){}())
.constructor.name; //=>MyConstructor
Whilst Object
is the mother of all in Javascript, you could extend it (there are pros and consto it)
虽然它Object
是 Javascript 之母,但您可以扩展它(它有利有弊)
Object.prototype.class = function(){
return this.constructor.name;
}
'some string'.class(); //=>String
(23).class(); //=>Number
// etc...
Note: javascript doesn't know 'classes'1, its inheritance model is prototypal
注意:javascript 不知道 'classes' 1,它的继承模型是原型
1from the ECMAScript standard.
1来自 ECMAScript 标准。
ECMAScript does not use classes such as those in C++, Smalltalk, or Java. Instead objects may be created in various ways including via a literal notation or via constructors which create objects and then execute code that initialises all or part of them by assigning initial values to their properties. Each constructor is a function that has a property named prototype that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new Date(2009,11) creates a new Date object. Invoking a constructor without using new has consequences that depend on the constructor. For example, Date() produces a string representation of the current date and time rather than an object.
ECMAScript 不使用诸如 C++、Smalltalk 或 Java 中的类。相反,可以通过各种方式创建对象,包括通过文字符号或通过构造函数创建对象,然后执行代码,通过将初始值分配给它们的属性来初始化它们的全部或部分。每个构造函数都是一个函数,它有一个名为prototype 的属性,用于实现基于原型的继承和共享属性。对象是通过在 new 表达式中使用构造函数创建的;例如, new Date(2009,11) 创建一个新的 Date 对象。在不使用 new 的情况下调用构造函数的后果取决于构造函数。例如,Date() 生成当前日期和时间的字符串表示而不是对象。
回答by amd
In js you can use :
在 js 中,您可以使用:
typeof
eq.
等式
var a="this is string";
typeof a; // return "string"
function abc(){}
typeof abc; // return "function"
var a = {a:1,b:2,c:3}
typeof a; return "object"
回答by Nemoden
typeof
and instanceof
is what you need
typeof
并且instanceof
是你所需要的
> x = "Hello, World!"
"Hello, World!"
> typeof x
"string"
You can check constructors name to get a name of constructors class (or that what you call a class):
您可以检查构造函数名称以获取构造函数类的名称(或您称之为类的名称):
> x.constructor.name
> "String"