admin管理员组文章数量:1304843
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
?
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
?
-
They're rather indistinguishable for your goal. How to make fully indistingushable -- use
b = []
; – kirilloid Commented Jan 26, 2012 at 9:55 - @kirilloid - that might be a valid approach for some situations but in situations where say a factory function internally does the plumbing based on a prototypeParam, your suggestion is not an option. eg. in the creation of a monoid, start with: var monad = Object.create(prototypeParam); Here, it would be convenient if prototypeParam could be Array.prototype – arcseldon Commented Oct 12, 2014 at 11:52
2 Answers
Reset to default 5The short answer is no. This article explains it all in some detail.
No, you cannot. Object.create
is all about prototypes, but both []
and Object.create(Array.prototype)
inherit from the same prototype object.
What you call "desired Object.prototype.toString behaviour" is the internal [[Class]]
of the object, which is impossible to set up with Object.create
. Creating "true arrays" (with an Array
class and special array behaviour - indexed properties, .length
) is only possible by using array literals or calling the Array
constructor.
本文标签: v8Javascript Arrays created with Objectcreatenot real ArraysStack Overflow
版权声明:本文标题:v8 - Javascript Arrays created with Object.create - not real Arrays? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741794006a2397827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论