let obj = { "2": "a", "3": "b", length: 3, push: Array.prototype.push };
obj.push("c"); console.log(obj);
瀏覽器執行以上腳本后的結果 { "2": "a", "3": "c", length: 4, push: Array.prototype.push }
- push方法根據length屬性來決定從哪里開始插入給定的值。
push方法具有通用性。該方法和call()或apply()一起使用時,可以應用在類似數組的對象上。
如果length不能被轉成一個數組,則插入的元素的索引為0,包括length屬性不存在時。當length 不存在時,將會創建它。