如何从同一类创建2个不同的实例?

时间:2020-06-11 09:33:59

标签: javascript arrays object instance

如何在控制台中显示Cat类的两个实例:9岁的Skitty和6岁的Pixel?

(() => {
    class Cat {
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }
    }
    document.getElementById("run").addEventListener("click",() => {
    // your code here

    })

})();

在此先谢谢您,如果问题很愚蠢,我正在学习!

2 个答案:

答案 0 :(得分:0)

const instance1 = new Cat('Skitty', 9);
const instance2 = new Cat('Pixel', 6);
console.log(instance1, instance2);

答案 1 :(得分:0)

实例化并记录

我听得懂吗?

class Cat {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

console.log(
  new Cat('Skitty', 9),
  new Cat('Pixel', 6),
)