JS: Difference of Array vs Array-Like Object
Difference between array and array-like object
- For true array, value of
lengthwill automatically change. - For true array, its parent is Array.prototype. 〔see Prototype and Inheritance〕
- For true array, Array.isArray return true.
// create a array-like object const xalike = { 0: "a", 1: "b", length: 2 }; xalike[2] = "c"; // length is 3 if true array console.log(xalike.length === 2); console.log(Array.isArray(xalike) === false); // parent is not Array.prototype console.log(Reflect.getPrototypeOf(xalike) === Object.prototype); // s------------------------------ // real array let xra = ["a", "b"]; xra[2] = "c"; console.log(xra.length === 3); console.log(Array.isArray(xra)); console.log(Reflect.getPrototypeOf(xra) === Array.prototype);