Object Methods For JS Developer
Object.assign()
It is use to copy the values of all enumerable own properties from one or more objects to target object. Objects are assigned and copied by reference.
Object.assign(target, sources)
const object1 = {
a : 11;
b : 22;
c : 33;
}
const object2 = object.assign({c : 4, d : 25}, object1)
console.log(object2.c, object2.d) // 33 25
Object.create()
The method is use to create a new object with specified prototype object and properties.
Object.create(prototype [, propertiseObject])
Const people = {
printIntroduction : function (){
console.log(`My name is $(this.name). Am I Human $(this.human) `) ;
}
};
const me = Object.create(people);
me.name = "Dipak";
me.human = true;
me.printIntroduction();
Object.freeze()
This method freezes an object that prevents new properties from begin added to it. This method prevents the modification of existing, property and values.
const video = { subscribeStatus: true }
const freezeObj = Object.freeze(video)
freezeObj.subscribeStatus = false
console.log(
`you are ${
freezeObj.subscribeStatus ? 'Subscribed' : 'Not Subscribed'
} to Dipak`
)
Object.entries()
This method is used to return an array of given object's own property [key,value] pairs.
const obj = { 10: 'instagram', 20: 'Facebook', 30: 'Whats App' };
console.log(Object.entries(obj)[2]); // ["30","Whats App"]
Object.values()
This method returns an array which contains the given object's own enumerable property values, in the same order as that provided by for...in loop.
const object1 = {
a: 'Dipak',
b: 25,
c: true
}
console.log(Object.values(object1)) // ["Dipak", 25, true]