JS私有類字段

class Dog {
    #sound = 'Woof! Woof!'; // this is private
    makeSound() {
        console.log( this.#sound );
    }
}
// create instance
const tommy = new Dog();
tommy.makeSound() // Woof! Woof!
//console.log( tommy.#sound ); // SyntaxError
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章