Lesson:
Object methods
List of content:
Object methods
Types and examples
Object Methods:
Actions on objects are carried out using methods. An object Property that includes a function declaration is
known as an object method.
Types of object methods
Object.keys(
Object.values(
Object.entries(
Object.assign(
Object.freeze(
Object.seal()
Object.keys():
It is a method that returns an array of an object's own property names.
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
var keys = Object.keys(emp);
console.log(keys);
Output:
Object.values():
Object.values() is a method that returns an array of an object's own property values.
Full Stack Web Development
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
var rec = Object.values(emp);
console.log(rec);
Output:
Object.entries()
This method is used to return an array of enumerable property [key, value] pairs of the object passed
as the parameter.
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
console.log(Object.entries(emp)[1]);
Output:
Object.assign():
The values and properties of one or more source objects are copied to a destination object using the
Object.assign() function.
Full Stack Web Development
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
const emp_obj = Object.assign({},emp)
console.log(emp_obj);
Output:
Object.freeze():
An object is frozen using this method.
Changing a frozen object is impossible. It prevents the addition and deletion of properties. Additionally, it
prevents changes to property values from occurring unless an object is involved.
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
Object.freeze(emp);
console.log(Object.isFrozen(emp));
Output:
Object.seal():
It is a method identical to Object.freeze(). You cannot add or remove an object's properties, but you can
edit the value of an existing property.
Full Stack Web Development
var emp = {
name: 'Alex',
age: 27,
salary:10000
};
Object.seal(emp);
console.log(Object.isSealed(emp));
Output:
Full Stack Web Development