W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript Getter 和 Setter</h1> <p>Getter 和 Setter 允许您通过方法获取和设置属性。</p> <p>此例具有修改的 setter 以保护语言的大写更新。</p> <p id="demo"></p> <script> // Create an object: var person = { firstName: "John", lastName : "Doe", language : "", set lang(value) { this.language = value.toUpperCase(); } }; // Set an object property using a setter: person.lang = "en"; // Display data from the object: document.getElementById("demo").innerHTML = person.language; </script> </body> </html>