class MovieCollection extends Array {
constructor(name, ...items){ // using rest to get all other items
super(...items); // using spread to make them into array
this.name = name; /* assigning name */
}
//method to add new movie to the array
add(movie){
this.push(movie);
}
}
const movies = new MovieCollection("col1",
{
title: "hello",
rating: 10
},
{
title: "hello3",
rating:40
}
);
movies.add("asas");
console.log(movies);
我正在扩展数组以添加项目。基本上new Array();
不知道为什么这段代码不起作用。这可能是一种不好的做法,但这只是为了学习。
错误似乎是添加功能。