Enhanced typeof() operator in JavaScript

Javascript is weakly typed, and its type system always behaves different than your expectation.
Javascript provide typeof operator to test the type of a variable. it works fine generally. e.g.

typeof default behaviors
1
2
3
4
5
6
typeof(1) === 'number'
typeof('hello') === 'string'
typeof({}) === 'object'
typeof(function(){}) === 'function'

But it is not enough, it behaves stupid when you dealing with objects created by constructors. e.g.
if you expected

Expected typeof behavior against object
1
typeof(new Date('2012-12-12')) === 'date' // Returns false

Then you must be disappointed, since actually
Actual typeof behavior against object
1
typeof(new Date('2012-12-12')) === 'object' // Returns true

Yes, when you apply typeof() operator on any objects, it just yield the general type “object” rather than the more meaningful type “date”.

How can we make the typeof() operator works in the way as we expected?
As we know when we create a object, the special property of the object constructor will be set to the function that create the object. which means:

Get constructor property of object
1
(new Date('2012-1-1')).constructor // Returns [Function Date]

So ideally we can retrieve the name of the function as the type of the variable. And to be compatible with javascript’s native operator, we need to convert the name to lower case. So we got this expression:

Simulate typeof operator behavior with constructor property
1
2
3
4
5
function typeOf(obj) { // Use capital O to differentiate this function from typeof operator
return obj ? obj.constructor.name.toLowerCase() : typeof(obj);
}
typeOf(new Date('2012-1-1')) === 'date' // Returns true

And luckily, we can also apply this to other primitive types, e.g:

Apply typeOf to primitive types
1
2
3
typeOf(123) === 'number'; // Returns true
typeOf('hello') === 'string'; // Returns true
typeOf(function(){}) === 'function'; // Returns true

or even

Apply typeOf to anonymous object
1
typeOf({}) === 'object'; // Returns true

So in general, we use this expression as new implementation of the typeof() operator! EXCEPT One case!

If someone declare the object constructor in this way, our new typeof() implementation will work improperly!

Closure encapsulated anonymous constructor
1
2
3
4
5
var SomeClass = (function() {
return function() {
this.someProperty='some value';
}
})();

or even define the constructor like this

Anonymous Closure
1
2
3
var SomeClass = function() {
this.someProperty = 'some value';
}

And we will find that

Apply typeOf to object instantiated by anonymous constructor
1
typeOf(new SomeClass) === ''; // Returns true

the reason behind this is because the real constructor of the SomeClass is actually an anonymous function, whose name is not set.

To solve this problem, we need to declare the name of the constructor:

Closure encapsulated named constructor
1
2
3
4
5
var SomeClass = (function() {
return function SomeClass() {
this.someProperty='some value';
}
})();

or even define the constructor like this

Named constructor
1
2
3
var SomeClass = function SomeClass() {
this.someProperty = 'some value';
}