A In Depth Information Of JavaScript Object & Its Methods
Declaration of Object, Accessing Elements, Methods, User Defined Methods, Built-In Methods
Introduction
JavaScript object is a non-primitive data type that allows you to store multiple collections of key/value pairs and more complex entities.
Object Declaration
Here, an object Object name
is defined. Each member of an object is a key
: value
pair separated by commas and enclosed in curly braces {}
.
Example:
const user = {
firstName: 'John',
lastName: 'Due',
};
Accessing Elements
To get a Complete Object
const user = {
firstName: 'John',
lastName: 'Due',
};
console.log(user)
Output:
{
firstName: 'John',
lastName: 'Due',
}
To get an element from an Object
const user = {
firstName: 'John',
lastName: 'Due',
};
console.log(user.firstName) // dot notation
// OR
console.log(user['firstName']) // bracket notation
Output:
'John'
Here, it calls the objects
key
to get its value.
To get an element from an Object of another Object
const user = {
name:{
firstName: 'John',
lastName: 'Due',
}
};
console.log(user.name.firstName)
Output:
'John'
Methods
The JavaScript method is an object property that has a function value
.
User Defined Methods
Declaring Methods
const user = {
name: function(){
return "John"
}
};
Accessing Methods
const user = {
name: function(){
return "John"
}
};
console.log(user.name())
Output:
John
Built-In Methods
Object.create()
The Object.create() method is used to create a new object and link it to the prototype of an existing object.
Object.create(proto)
const user = {
firstName: 'John',
lastName: 'Due',
};
const person1 = Object.create(user);
console.log(person1.firstName)
Output:
John
Object.values()
Object.values() creates an array containing the values of an object.
Object.values(obj)
const user = {
firstName: 'John',
lastName: 'Due',
};
const name = Object.values(user);
console.log(name)
Output:
[ 'John', 'Due' ]
Object.keys()
Object.keys() creates an array containing the keys of an object.
Object.keys(obj)
const user = {
firstName: 'John',
lastName: 'Due',
};
const name = Object.keys(user);
console.log(name)
Output:
[ 'firstName', 'lastName' ]
Object.entries()
Object.entries() creates a nested array of the key/value pairs of an object.
Object.entries(obj)
const user = {
firstName: 'John',
lastName: 'Due',
};
const name = Object.entries(user);
console.log(name)
Output:
[
[ 'firstName', 'John' ],
[ 'lastName', 'Due' ]
]
Object.fromEntries()
Object.fromEntries() creates an object array a nested array.
Object.fromEntries(obj)
const user = [ [ 'firstName', 'John' ], [ 'lastName', 'Due' ] ]
const name = Object.fromEntries(user);
console.log(name);
Output:
{
firstName: 'John',
lastName: 'Due'
}
Object.freeze()
Object.freeze() prevents modification to properties and values of an object.
Object.freeze(obj)
const user = {
firstName: 'John',
lastName: 'Due',
};
const name = Object.freeze(user);
name.firstName= 'Mario'
name.middleName= 'Bros'
console.log(name)
Output:
{ firstName: 'John', lastName: 'Due' }
Object.seal()
Object.seal() prevents new properties from being added to an object, but allows the modification of existing properties.
Object.seal(obj)
const user = {
firstName: 'John',
lastName: 'Due',
};
const name = Object.seal(user);
name.firstName= 'Mario'
name.middleName= 'Bros'
console.log(name)
Output:
{ firstName: 'Mario', lastName: 'Due' }
Object.assign()
Object.assign() is used to copy values from one or objects to another object.
Object.assign(destination_objects, ...source_objects)
const name1 = {
firstName: 'John',
};
const name2 = {
lastName: 'Due',
};
Object.assign(name2, name1); // Also returns value of name2
console.log(name2)
Output:
{ lastName: 'Due', firstName: 'John' }
Object.is()
The Object.is() method determines whether two values are the same or not.
Object.is(value1, value2)
const user = { firstName: 'John', lastName: 'Due' }
const obj = Object.is(user.firstName, 'John');
console.log(obj);
Output:
true