首页 > 解决方案 > 如何从同一类创建 2 个不同的实例?

问题描述

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

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

    })

})();

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

标签: javascriptarraysobjectinstance

解决方案


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

推荐阅读