【发布时间】:2021-05-10 15:56:48
【问题描述】:
我仍在尝试学习,并且尝试将一组演员添加到电影类中,我做到了,但我仍然遇到问题,因为如果您添加另一个演员最后一个消失了,我尝试使用循环但是我什么都做不了。
class Movie {
constructor(title,year,duration){
this.title = title;
this.year = year;
this.duration = duration;
}
addCast(actors){
this.actors = actors
}
}
class Actor {
constructor(name,age)
{
this.name = name;
this.age = age;
}
}
const terminator = new Movie('Terminator I', 1985, 60);
const arnold = new Actor('Arnold Schwarzenegger', 50);
const otherCast = [
new Actor('Paul Winfield', 50),
new Actor('Michael Biehn', 50),
new Actor('Linda Hamilton', 50)
];
//From here it can not be modified
let movieOne = new Movie("Kong","2018","2h30m");
let movieTwo = new Movie("Joker","2019","2h03m");
let movieThree = new Movie("John Wick 3", "2019", "1h49m");
terminator.addCast(arnold);
terminator.addCast(otherCast);
//To here it can not be modified
console.log({movieOne,movieTwo,movieThree,terminator});
看到了吗?阿诺德也应该在演员中,但不是!提前感谢您的帮助。
另一件事,这是一个练习,我不能修改我评论的行。
【问题讨论】:
标签: javascript class object prototype