运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往空间
<!DOCTYPE html> <html> <body> <h1>JavaScript Objects</h1> <h2>The Object.defineProperty() Method</h2> <p>This example uses the defineProperty() method to add a getter and a setter.</p> <p id="demo"></p> <script> // Create an object: const person = { firstName:"John", lastName:"Doe" }; // Define a getter Object.defineProperty(person, "fullName", { get: function () {return this.firstName + " " + this.lastName;} }); // Display full name document.getElementById("demo").innerHTML = person.fullName; </script> </body> </html>