1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function type(o){
   return !!o && Object.prototype.toString.call(o).match(/(\w+)\]/)[1];
}
 
type(101);          // returns 'Number'
type('hello');      // returns 'String'
type({});           // returns 'Object'
type([]);           // returns 'Array'
type(function(){}); // returns 'Function'
type(new Date());   // returns 'Date'
type(document);     // returns 'HTMLDocument'
 
// Например, проверяем масив:
if( type([1,2,3,4,5]) === 'Array' ) {
   doStuff();
}

источник: james.padolsey.com

Оставьте свой комментарий