DEV Community

Discussion on: When is an array, not an array?

Collapse
 
joelnet profile image
JavaScript Joel

This Array is not an Array!

const objArray = { 0: 'first', 1: 'second', 2: 'third', length: 3 }
objArray.__proto__ = Object.create(Array.prototype)

objArray.map(x => x.toUpperCase()) //=> ​​​​​[ 'FIRST', 'SECOND', 'THIRD' ]​​​​​
objArray.push('fourth')

objArray //=> Array { [Iterator]  0: 'first', 1: 'second', 2: 'third', 3: 'fourth', length: 4 }​​​​​

Object.prototype.toString(objArray) //=> "[object Object]"
Collapse
 
thejoezack profile image
Joe Zack

Haha, that is disgusting! I love it!