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'
 
    
    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.
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"
In 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"
