Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Object</h1> <h2>The 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 fullName document.getElementById("demo").innerHTML = person.fullName; </script> </body> </html>