js私有变量 2024-07-30- 2025-09-1712345678910111213141516171819202122232425<html><script> class Home { #money; // 声明私有变量 constructor(money) { this.#money = money || 0 } #showMoney() { console.log(this.#money) } makeMoney(money) { this.#money += money } } var foo = new Home(100); console.log(foo); console.log(foo.#money); // 报错</script></html> 如果直接在chrome开发工具的控制台执行js是可以拿到私有变量的,是为了方便开发调试,如果在html中是拿不到的 I'm so cute. Please give me money.Post author: Look LiPost link: https://blog.look.cloudns.biz/js%E7%A7%81%E6%9C%89%E5%8F%98%E9%87%8F/Copyright Notice: All articles in this blog are licensed under unless otherwise stated.