Javascript Arrays created with Object.create - not real Arrays?

后端 未结 2 1143
面向向阳花
面向向阳花 2020-12-06 02:47

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 = [         


        
相关标签:
2条回答
  • 2020-12-06 03:00

    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.

    0 讨论(0)
  • 2020-12-06 03:23

    The short answer is no. This article explains it all in some detail.

    0 讨论(0)
提交回复
热议问题