I'm learning javascript. The book I'm reading told me that instanceOf cannot identify an Array object when values are passed back and forth between frames in the same web page because each web page has its own global context—its own version of built-in types. And therefore ECMAScript 5 introduced Array.isArray(), which we should use. Very clear explanation. My question is why there is no similar methods for the other build-in types (such as Date, RegExp). How can we safely identify them in web page with multiple frames.
You can use Object.prototype.toString
:
function typeOf(obj) {
return {}.toString.call(obj).slice(8,-1);
}
typeOf(obj) == 'Date'
typeOf(obj) == 'RegExp'
...