Possible Duplicate:
How to get a JavaScript Object's Class?
In Ruby I could do this to check the class of an instance:
'this is an string'.class
=>String
Is there any similar thing in js?
Possible Duplicate:
How to get a JavaScript Object's Class?
In Ruby I could do this to check the class of an instance:
'this is an string'.class
=>String
Is there any similar thing in js?
You probably mean type or constructor, not class. Class has a different meaning in 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 '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".
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).
You can test the constructor with 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.
new Date().constructor.name; // => 'Date'
Function.prototype.call.bind(Object.prototype.toString); - this is really nice. +1Function.prototype.call.bind(Object.prototype.toString) won't work for instances of custom constructors: getClassOf(new function CC(){}()) returns [object Object]class, which is Object. If you want the constructor you use instanceof or constructor.name. It's not a matter of whether it works. It's a matter of whether you understand what a class is in JavaScript (see my note at the beginning of the post, "You probably mean type or constructor, not class").instanceOf or the constructor property are reliable, and certainly not for host objects (like DOM objects). Not all javascript objects inherit from or are instances of Object (e.g. certain host objets in some browsers). If, in javascript, you find you need to discover what "class" an object is, you're probably going about things the wrong way.Date.prototype.getTime says it should check arguments based on [[Class]]. They use class, not constructor/proto, information. It can be helpful also in cross-frame objects. Why would you want to argue about expanding better knowledge and understanding of the intricacies of JavaScript?[Edit 2024] A small module to determine the type of (almost) everything
Not sure if this goes for all browsers, but you can use 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 cons to it)
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
1 from the ECMAScript standard.
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.
Object in js, and good reference for prototypeIn js you can use :
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"
typeof is more reliable than all the other suggested methods.typeof and instanceof is what you need
> 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"
object though, should it not? That won't tell you the name of the class (though not sure OP needs that).document.elementtypeof will return it. E.g. if y is not defined: typeof y -> "undefined". But I agree, typeof is not what the OP need, so, I updated my answer.var MyConstructor = function(){};var instance = new MyConstructor;console.log(instance.constructor.name);/*""*/