This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.



Smartphone icons created by Freepik - Flaticon

JS Object.defineProperty()

This example uses the defineProperty() method to add a getter and a setter.

// 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;