簡體   English   中英

如何從同一個 class 創建 2 個不同的實例?

[英]How to create 2 different instances from the same class?

如何在控制台中顯示 class Cat 的兩個實例:Skitty,9 年和 Pixel,6 年?

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

    })

})();

提前致謝,如果問題很愚蠢,我很抱歉,我正在學習!

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

實例化和記錄

我理解得好嗎?

 class Cat { constructor(name, age) { this.name = name; this.age = age; } } console.log( new Cat('Skitty', 9), new Cat('Pixel', 6), )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM