如何将字符串添加到调用它的构造函数创建的对象的数组中?

How do I add a string to an array of a constructor created object it was called on?

提问人:MichaelKwon 提问时间:1/11/2023 更新时间:8/7/2023 访问量:334

问:

声明一个函数 Dog,该函数在使用 new 关键字调用时创建 Dog 对象的新实例。每个 Dog 对象都应具有 name 属性和 breed 属性,这两个字符串在调用 Dog 函数时作为参数传入。它还应该有一个名为 tricks 的属性,设置为一个数组,表示狗知道的所有技巧。启动新对象时,技巧应为空。

所有 Dog 对象还必须有权访问存储在构造函数原型上的两个方法:

第一种方法 learnTrick 应采用字符串作为参数,并将该字符串添加到调用它的特定 Dog 对象的 tricks 数组中。

第二种方法 performTrick 也应将字符串作为参数。它应该检查该字符串是否在属于它被调用的 Dog 实例的 tricks 数组中;如果是这样,它应该记录字符串“name performed trick!如果没有,请记录字符串 'name doesn't know that trick。

我尝试将字符串推送到创建的构造函数数组,但我可能错误地使用了点表示法。

   function Dog(name, breed) {
    this.name = name;
    this.breed = breed;
    this.tricks = [];
}

    Dog.prototype = {
    constructor: Dog,
    learnTrick: function (string1) {
        if (Dog.hasOwnProperty(string1) === false) {
            Dog.tricks = [string1];              //<---- I believe the problem lies here
            console.log(Dog.tricks);
        } else {
            tricks.push(string1)
            // console.log(tricks)
        }
    },
    performTrick: function (string2) {
        for (let property in Dog) {
            if (Dog.hasOwnProperty(string2)) {
                console.log(this.name + ' performed trick!');
            } else {
                console.log(this.name + ' doesn\'t know that trick.')
            }
        }
    }
}

    const fido = new Dog('Fido', 'poodle');

// Uncomment these lines to check your work!
    fido.learnTrick('fetch');
    fido.performTrick('fetch'); // should log 'Fido performed fetch!'
    fido.performTrick('sit'); // should log 'Fido doesn't know that trick.'`
JavaScript Arrays 方法 构造函数 原型

评论

0赞 Bergi 1/11/2023
如果您复制练习的文本,请将其格式化为引文,并注明出处
0赞 Bergi 1/11/2023
您需要在引用实例的方法中使用 ,而不是thisDog

答:

1赞 gog 1/11/2023 #1

你把事情复杂化了很多。Javascript 有语法,所有这些原型摆弄都是不必要的。代码应如下所示:Here's what your code should look like:class

class Dog {
    constructor(name, breed) {
        this.name = name
        this.breed = breed
        this.tricks = []
    }

    learnTrick(trick) {
        this.tricks.push(trick)
    }

    performTrick(trick) {
        if (this.tricks.includes(trick)) {
            console.log(`${this.name} performed ${trick}!`)
        } else {
            console.log(`${this.name} doesn't know how to ${trick}...`)
        }
    }
}

//

const fido = new Dog('Fido', 'poodle');

fido.learnTrick('fetch');
fido.performTrick('fetch');
fido.performTrick('sit');

0赞 LickingFilth 8/7/2023 #2

您可能正在寻找的“引擎盖下”版本(没有 Class 方法)是这样的:

function Dog(name, breed){
  this.name=name;
  this.breed=breed;
  this.tricks=[];
}
Dog.prototype.learnTrick=function(str){
  this.tricks.push(str);
}
Dog.prototype.performTrick=function(str){
  if(this.tricks.includes(str)){
    console.log(`${this.name} performed ${str}!`);
  }else{
    console.log(`${this.name} doesn't know that trick.`);
  }
}
const fido = new Dog('Fido', 'poodle');



// Uncomment these lines to check your work!
fido.learnTrick('fetch');
fido.performTrick('fetch'); // should log 'Fido performed fetch!'
fido.performTrick('sit'); // should log 'Fido doesn't know that trick.'