Table of Contents
You can add new properties to an existing object using Object.defineProperty():
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Management</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Adding a New Property</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe"
};
// Add a new property
Object.defineProperty(person, "fullName", {
value: "John Doe",
writable: true
});
document.getElementById("demo").innerHTML = person.fullName;
</script>
</body>
</html>

Result View Example
You can change a property value using Object.defineProperty():
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Management</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Changing a Property Value</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
// Change the age property
Object.defineProperty(person, "age", {
value: 35
});
document.getElementById("demo").innerHTML = "Age: " + person.age;
</script>
</body>
</html>

Result View Example
Property attributes control how properties behave:
writable - If true, the value can be changedenumerable - If true, the property shows up in enumeration (for...in)configurable - If true, the property can be deleted or changedObject.getOwnPropertyNames() returns all properties (including non-enumerable):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Management</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>List All Object Properties</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
eyeColor: "blue"
};
// Get all property names
const properties = Object.getOwnPropertyNames(person);
let text = "";
for (let prop of properties) {
text += prop + ": " + person[prop] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
Object.defineProperty() can also add getters and setters:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Management</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Adding Getters and Setters</h4>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe"
};
Object.defineProperty(person, "fullName", {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function(value) {
const parts = value.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
},
enumerable: true
});
document.getElementById("demo").innerHTML = "Before: " + person.fullName + "<br>";
person.fullName = "Jane Smith";
document.getElementById("demo").innerHTML += "After: " + person.fullName;
</script>
</body>
</html>

Result View Example
Using Object.defineProperty() to create a counter with controlled access:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Management</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>A Counter Example</h4>
<p id="demo"></p>
<script>
const obj = { counter: 0 };
Object.defineProperty(obj, "increment", {
get: function() {
this.counter++;
return this.counter;
}
});
Object.defineProperty(obj, "decrement", {
get: function() {
this.counter--;
return this.counter;
}
});
Object.defineProperty(obj, "reset", {
get: function() {
this.counter = 0;
return this.counter;
}
});
document.getElementById("demo").innerHTML =
"Initial: " + obj.reset + "<br>" +
"After increment: " + obj.increment + "<br>" +
"After increment: " + obj.increment + "<br>" +
"After decrement: " + obj.decrement + "<br>" +
"After reset: " + obj.reset;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.