It appears that the arrays created with Object.create are similar to arrays and quacks, such as arrays, but are still not real arrays. At least with v8 / node.js.
> a = [] [] > b = Object.create(Array.prototype) {} > a.constructor [Function: Array] > b.constructor [Function: Array] > a.__proto__ [] > b.__proto__ [] > a instanceof Array true > b instanceof Array true > Object.prototype.toString.call(a) '[object Array]' > Object.prototype.toString.call(b) '[object Object]'
Can any Javascript guru explain why this is so, and how to make my newly created array indistinguishable from a real array?
My goal is to clone data structures, including arrays, which can have custom properties. I could, of course, manually bind the properties to the newly created array using Object.defineProperty
, but is there a way to do this using Object.create
?
Philippe plantier
source share