It's not working because length is not defined here.
reduce() and for of need length property to work.
a = Object.create(Array.prototype, {
0 : { writable : true, configurable : true, value : 1},
1 : { writable : true, configurable : true, value : 2},
2 : { writable : true, configurable : true, value : 3},
3 : { writable : true, configurable : true, value : 4}
});
//Array {0: 1, 1: 2, 2: 3, 3: 4}
a.length = 4
//4
for(let i of a)
console.log(i);
// 1
// 2
// 3
// 4
But it will still not fool the Array.isArray()
Array.isArray(a)
//false
This is because the hidden machanisim of "Execotic Object"
Exotic Object can not be created with Object.create() or new Constructor(), this needs to be extended with javascript class to create subclass ref
Here the internal mechanisim for Array is writtine inside JS Engine itself so it untouched.
Exotic Objects
Are any objects that have different internal implementations are Exotic Object, for example: Array, Proxy, String, Arguments, Module
So, in Javascript objects can be categorized into Ordinary Objects and Exotic Objects.
Ordinary objects are normal object we encounter that doesn't show any unexpected behavior. example: any objects initialized with {}
Let's see some exotic object's behavior
const arr = []
//undefined
arr[0] = 10
//10
arr.length
//1
arr[NaN] = 18
//18
arr[null] = 20
//20
arr.length
//1
arr[50] = 50
//50
arr.length
//51
console.log(arr)
//[10, empty × 49, 50, NaN: 18, null: 20]
//0: 10 50: 50 NaN: 18 null: 20 length: 51 [[Prototype]]: Array(0)
arr.length=0
//0
console.log(arr)
//[NaN: 18, null: 20]
//NaN: 18 null: 20 length: 0 [[Prototype]]: Array(0)
Do you wonder why it is happening?
Changing length affects the array elements and changing elements affects the array
This is because array have [[DefineOwnProperty]] different then of that Object.defineProperty()
link
So, arr[index] the index has to be in the range of 0 to (2^32)-1 and should not be the word length then it will be treated as an index, and length will be updated if index >= length. Else it will be added as a normal object property.
link
If now index is the word length ArraySetLength function is called and if previous length is writable and new length is greater than previous it will set the length o array as provided and empty cells will be showed as empty by the browser or node.
Esle if new length is smaller it will delete all the element until it encounter an non-configurable element
const arr = [10, 20,30,50]
//undefined
Object.defineProperty(arr,1, {value: 15, configurable : false});
//[10, 15, 30, 50]
arr.length=0
//0
console.log(arr)
//[10, 15]
YouTube video explaination link