运行 ❯
获取您的
自有
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 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>This example creates a getter for a property called fullName.</p> <p id="demo"></p> <script> // Create an object: var person = { firstName: "John", lastName : "Doe", get fullName() { return this.firstName + " " + this.lastName; } }; // Display data from the object using a getter: document.getElementById("demo").innerHTML = person.fullName; </script> </body> </html>