【问题标题】:Class object overlapping类对象重叠
【发布时间】: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


    【解决方案1】:

    你有

    addCast(actors){
        this.actors = actors
    }
    

    这不会添加传递的actor数组到实例上的actors - 它替换实例的actors与传递的参数。调用addCast 将导致actors 上先前存在的任何内容丢失。

    为了帮助减少错误,它可以帮助适当地命名方法 - 对于这样的逻辑,我称之为setCast,而不是addCast

    如果您想添加到现有演员表的末尾,并且您不确定参数是要添加的单个演员还是要添加的演员数组,请使用:

      addCast(actorOrActors) {
        if (Array.isArray(actorOrActors)) {
          this.actors.push(...actorOrActors);
        } else {
          this.actors.push(actorOrActors);
        }
      }
    

    class Movie {
      constructor(title, year, duration) {
        this.title = title;
        this.year = year;
        this.duration = duration;
        this.actors = [];
      }
    
      addCast(actorOrActors) {
        if (Array.isArray(actorOrActors)) {
          this.actors.push(...actorOrActors);
        } else {
          this.actors.push(actorOrActors);
        }
      }
    }
    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
    });

    【讨论】:

    • 嗨,我可能做了一些非常明显的错误,但我是新手。您更改了参数名称以避免与演员一词混淆,对吗?然后我尝试只是暴力复制粘贴您的代码,但我无法读取 64 和 89 处未定义的属性推送(添加阿诺德),这是我在这里丢失的东西吗?
    • 查看答案中的实时 sn-p,它可以按需要工作 - 确保将 this.actors 初始化为构造函数中的空数组,以便调用 addCast 改为推送到该数组完全替换它。
    【解决方案2】:

    这是因为在您的 addCast() 方法中,每次调用它时,您都是在替换之前的值而不是附加它

    【讨论】:

    • 是的,你是正确的,但我不知道如何正确地做到这一点。这种添加而不是重叠对象的方法是如何命名的?
    【解决方案3】:

    您使用第二个 addActors 调用覆盖了 arnold。 一次只能将一个演员添加到一组演员。

    class Movie {
    constructor(title,year,duration){
        this.title = title;
        this.year = year;
        this.duration = duration;
        this.actors = [];
    }
    
    
    addCast(actor){
        this.actors.push(actor);
    }
    
    terminator.addCast(arnold);
    terminator.addCast(otherCast[0]);
    terminator.addCast(otherCast[1]);
    terminator.addCast(otherCast[2]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2010-09-08
      相关资源
      最近更新 更多