定义的 add 方法,调用的是 push 方法...
为什么把 push 改回 add 会报方法不存在?
class PersonCollection extends Array {
constructor(name, ...items) {
super(...items);
this.name = name;
}
add(item) {
this.push(item);
}
}
const collection = new PersonCollection('name',
{'name': 'zhangsan'},
{'name': 'lisi'}
);
collection.add({'name': 'wangwu'});
console.log(collection);
//Uncaught TypeError: collection.add is not a function