totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Make U.isArray() more reliable (multi-frame context) #661

Closed exside closed 6 years ago

exside commented 6 years ago

The problems with using instanceof Array arises when it comes to scripting in multi-frame DOM environments. In a nutshell, Array objects created within one iframe do not share [[Prototype]]’s with arrays created within another iframe. Their constructors are different objects and so both instanceof and constructor checks fail. The proposed solution is not dependent on frames (since it checks internal [[Class]]) and is more robust than duck-typing approach (e.g. checking for Prototype methods like .slice() that are unique to the Prototye).

Great writeup here: http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

PS: not sure which branch to target, can change if v3.0.1 is the wrong one!

petersirka commented 6 years ago

Hi @exside, thank you very much.

I don't agree because:

Instanceof vs toString

I think that the current solution is very good.