It looks like Arrays created with Object.create walk like Arrays and quack like 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 some Javascript guru explain why it is so, and how to make it so that my newly created array is indistinguishable from a real array?
My goal here is to clone data structures, including Arrays which may have custom properties attached. I could, of course, manually attach properties to a newly-created Array using Object.defineProperty
, but is there a way to do so using Object.create
?
The short answer is no. This article explains it all in some detail.