运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,暗/亮
前往 Spaces
<!DOCTYPE html> <html> <body> <h2>JavaScript Getters and Setters</h2> <p>Getters and setters allow you to get and set properties via methods.</p> <p id="demo"></p> <script> // Create an object: const person = { firstName: "John", lastName: "Doe", language: "", set lang(lang) { this.language = lang.toUpperCase(); } }; // Set a property using set: person.lang = "en"; // Display data from the object: document.getElementById("demo").innerHTML = person.language; </script> </body> </html>