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.
Asked
Active
Viewed 107 times
1
-
use duck typing to avoid splitting hairs over constructors and worry about needed capabilities. – dandavis Feb 25 '14 at 04:03
-
possible duplicate of [How do you check if a variable is an array in JavaScript?](http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript) – ced-b Feb 25 '14 at 04:04
1 Answers
1
You can use Object.prototype.toString
:
function typeOf(obj) {
return {}.toString.call(obj).slice(8,-1);
}
typeOf(obj) == 'Date'
typeOf(obj) == 'RegExp'
...

elclanrs
- 92,861
- 21
- 134
- 171