How to Update an Object in JavaScript
In JavaScripot, updating a variable is quite straitforward. We assign a nnew value to the variable like this.
var name = "John"; name = "Joe"; //variable name has been chagned from John to Joe.
Waht about an object?
Updating a property of an object is the same way we do with varialbes.
<script>
var obj = {
name:"John",
age:10,
call:function(){
console.log("My name is " + this.name + ".");
}
};
obj.age = 11;
console.log(obj.age);
// 11
</script>
Updating mothods in an object
We can also update the methods the same we way we do with updating object property
<script>
var obj = {
name:"John",
age:10,
call:function(){
console.log("My name is " + this.name + ".");
}
};
obj.call();
//My name is John.
obj.call = function(){
console.log("I'm " + this.age + " years old.");
}
obj.call();
//I'm 11 years old.
</script>
Creating new object properties and methods
We can create new object properties and methods with the same syntax as the updating syntxs above if
the property or method does not exit.
<script>
var obj = {
name:"John",
age:10,
call:function(){
console.log("My name is " + this.name + ".");
}
};
obj.call();
//My name is John.
obj.call = function(){
console.log("I'm " + this.age + " years old.");
}
obj.height = 100;
console.log(obj.height);
// 100
obj.tellHeight = function(){
console.log("I'm " + this.height + " feet tall.");
};
obj.tellHeight();
// I'm 100 feet tall.
</script>
Using Object.dfinceProperty() method
We can update or create a single object property or method using the Object.DefinceProperty() method.
<script>
var obj = {
name:"John",
age:10,
call:function(){
console.log("My name is " + this.name + ".");
}
};
obj.call();
//My name is John.
obj.call = function(){
console.log("I'm " + this.age + " years old.");
}
obj.height = 100;
console.log(obj.height);
// 100
obj.tellHeight = function(){
console.log("I'm " + this.height + " feet tall.");
};
Object.defineProperty(obj,"weight",{value:120 + " lbs"});
console.log(obj.weight);
//120 lbs
</script>
Using Object.definceProperties() methods
We can also update or create multiple object properties or methods using the Object.definceProperties() method.
<script>
var obj = {
name:"John",
age:10,
call:function(){
console.log("My name is " + this.name + ".");
}
};
obj.call();
//My name is John.
obj.call = function(){
console.log("I'm " + this.age + " years old.");
}
obj.height = 100;
console.log(obj.height);
// 100
obj.tellHeight = function(){
console.log("I'm " + this.height + " feet tall.");
};
Object.defineProperties(obj,{a:{value:"Joe"},sayHi:{value:function(){console.log("Hi, " + this.a)}}});
obj.sayHi();
//Hi, Joe
</script>
Updating the getter and setter
The object getter and setter can not be updated by direct assigning a new value to the getter or setter.
We need to use the Object.defineProperty() or the Object.defineProperties() methods.
<script>
var shoppingCart = {
count:5,
price:100,
}
//Object.defineProperty(shoppingCart,"totalPrice",{get:function(){return this.count * this.price;}})
//or
Object.defineProperties(shoppingCart,{totalPrice:{get:function(){return this.count * this.price;}}})
console.log(shoppingCart.totalPrice);
//500
</script>
Create an Updating Method
When we want to update properties in an object, we can do it via its own methods. Together
with the getter we can easily maange data in the object.
The total price is $0
<button onclick="shoppingCart.buy();showTotal.innerHTML = shoppingCart.totalPrice">Buy</button>
<div>The total price is $<span id="showTotal">0</span></div>
<script>
var shoppingCart = {
count:0,
price:100,
buy:function(){
this.count++
},
get totalPrice(){
return this.count * this.price;
}
};
</script>
Comments
Post a Comment