SteveSanderson / knockout-es5

Knockout.js meets ECMAScript 5 properties
157 stars 39 forks source link

instanceof shouldn't be used #14

Closed brigand closed 9 years ago

brigand commented 10 years ago

To make it iframe safe, instead of

foo instanceof Array

use

foo && foo.constructor.name === "Array"
jods4 commented 9 years ago

Don't do this please! constructor.name is not compatible with IE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name.

I found this out the hard way by using @brigand's fork to get a few fixes (notably AMD support) and getting very weird behaviour with arrays.

The correct way to check if something is an array in ES5 browsers (it won't work before IE9, but it's ok in this library) is to use the native Array.isArray(obj), see MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray