简体   繁体   中英

How to create 2 different instances from the same class?

How can I display, in the console, two instances of the class Cat: Skitty, 9 years and Pixel, 6 years?

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

    })

})();

Thanks in advance and sorry if the question in silly, I'm learning !

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

instantiate and log

did I understood well?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM