I'd like to understand how Arrays are specified and built.
I'm mixing arrays with extra methods and trying to pass in Array.isArray(). I can pass instanceof and Poly-filled isArray
var protoOfArray = Object.getPrototypeOf([])
var tools4Arrays = Object.defineProperty(
Object.create(protoOfArray),
'sum', {
'value': function sum() {
return this.reduce((a, b) => a + b, 0)
}
}
)
tools4Arrays[Symbol.toStringTag] = 'Array'
var fakeArray = Object.defineProperty(
Object.create(
tools4Arrays
),
'length', {
'value': 0,
'writable': true
}
)
console.log(`fakeArray instanceof Array: ${fakeArray instanceof Array}`)
console.log(`polyfill.isArray(fakeArray): ${Object.prototype.toString.call(fakeArray) === '[object Array]'}`)
console.log(`Array.isArray(fakeArray): ${Array.isArray(fakeArray)}`)
If I use: fakeArray = Object.setPrototypeOf([], tools4Arrays) the last log is true too.
What else separates arrays of objects? Can I mock it?