Why does "push method" works with object? How that mechanism works underhood?
function MyArray() { }
MyArray.prototype = [];
var arr = new MyArray();
arr.push(1, 2, 3);
console.log(arr); // [1, 2, 3] in Chrome
sorry for my english. Thanks!
Why does "push method" works with object? How that mechanism works underhood?
function MyArray() { }
MyArray.prototype = [];
var arr = new MyArray();
arr.push(1, 2, 3);
console.log(arr); // [1, 2, 3] in Chrome
sorry for my english. Thanks!
MyArray returns an object, even in Chrome, and uses the methods of Array, via assigned prototypal inheritance. The result of an instance of MyArray is still an object, not an array.
function MyArray() { }
MyArray.prototype = [];
var arr = new MyArray();
arr.push(1, 2, 3);
console.log(arr); // { 0: 1, 1: 2, 2: 3, length: 3 }
console.log(typeof arr); // object
console.log(Array.isArray(arr)); // false
console.log(arr instanceof Array); // true
console.log(arr instanceof MyArray); // true