Bugs of `instanceof` and `iFrame`

Checking either the objects is Array or Objects is a very common job in nowadays. For the most common way is using `instanceof`

myArr instanceof Array   // if myArr is array return true

It works perfect in most way, EXCEPT when you are using it inside iFrame. Multiple frame in single page will cause instanceof to detect Array as Object. It is absolutely pain…

Solution

Yet, you can still checking Array inside iFrame without using instanceof by Object.prototype.toString.call method. It will returned ‘[object Array]’ if myArr is Array.

 Object.prototype.toString.call(myArr) === '[object Array]' 

If you want to know more about the bugs, check it out kangax blogs. This site is more informative on it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s