JavaScript object methods are the functions that help to make a connection with objects that make it possible to perform actions on those objects. A JavaScript object performs like a container which allows us to hold various pieces of information within a single structure. Methods permit objects to make documents of their behavior and help to make a connection with their data.
The Syntax of JavaScript Objects:
objectName.methodName()
Explanation
objectName(): This is the reference of the object.
methodName(): It is already defined action saved inside the object that can be used when we want.
Example
Code:
const name = {
f_Name: "Tpoint",
l_Name: "Tech",
age: 30,
full_Name: function() {
return this.f_Name + " " + this.l_Name;
}
};
console.log(name.full_Name());
Output:
Tpoint Tech
Explanation:
f_name and l_name are stored in the object name. It uses this keyword to send the object itself and assess its f_Name and l_Name properties. When we call the function name that runs the method and produces the result of the complete name.
How to create a JavaScript Object?
In order to make a new JavaScript object based on an object that is already present, we use Object.create() method which makes a new object copy with the help of the inheritance feature. Inheritance is allowed to make a JavaScript Object with the help of an Object.create() method. A new object takes the properties and methods from the main method by utilizing this method.
The Syntax is:
Object.create()
Explanation
The Object.create() method helps us build a new object that inherits properties from an existing one which permits us to reuse the original object's features. This method is static.
Example
Code:
let Stud = {
name: "Adon",
age: 25,
marks: 77.2,
display() {
console.log("Name:", this.name);
}
};
let std1 = Object.create(Stud);
std1.name = "David";
std1.display();
Output:
Name: David
Explanation
We create a new object named std1 using Object.create(Stud). This command makes a new object that uses inheritance to inherit the features from the Stud method. By using this method, std1 uses all the properties and behaviour of the Stud method.
JavaScript Object properties:
JavaScript Object properties help us to modification in data. JavaScript objects contain many properties of any data type. To access these properties we can use notations. With the help of the object properties, we can update data like change, add, delete and read.
Example:
const man={
name: "Johnson",
age:27,
}
Explanation:
Here, the name Johnson and age 27 both are properties of the object man.
How to Access Object Properties?
Accessing properties of an object is a fundamental feature in various programming languages enabling users to access or modify the values stored within key-value pairs. An object is an organized data collection with each key being a string and its value a number, string, array, object or any other data type.
There are two methods to access these properties:
1. Dot Notation:
It is the most commonly used approach when the property name is in a single word.
Demo:
const dog = {
breed: "Labrador"
}
console.log(dog.breed);
Output:
Labrador
Explanation
In the above code, an object dog is created using the const keyword. The Object has one property called breed and its value is to set string Labrador. This property shows the breed of the dog.
2. Bracket Notation:
Bracket Notation is a way to change or update a value using a bracket.
Demo:
let candidate = {
"first_name": "David",
age: 28,
city: "Paris"
};
console.log(candidate["first_name"]);
let property = "city";
console.log(candidate[property]);
Output
David
Paris
Explanation
In the above code, an object is created named a candidate with three properties. first_name is the string key value of the first name David, the age is a number key value of the age is 28, and the city is also a string key value of the city Paris. To access the property of the object, we use bracket notation that prints the value of the first name. We create a property variable and assign value city.
JavaScript Object Operations:
In JavaScript, there are many operations that can be performed on objects to provide the quality of the dynamic programming. With the help of the operations, programmer can update in data. Operations on objects involve creating, reading, writing and deleting properties which are commonly known as CRUD operations.
1. Update Object Property:
With the help of the update object property, we can update the key value.
Demo:
const myObj = {
name: "Bob",
age: 27,
city: "New York"
};
myObj.age = 28;
myObj.city = "Paris";
console.log(myObj);
Output
{ name: 'Bob', age: 28, city: 'Paris' }
Explanation
In the above code, we create an object with the help of a const keyword that has three properties name, age and city. We can update the properties of the object with the usage of Dot notation and receive the updated value of the object property.
2. Delete Object Property:
This property is mainly utilized to delete both the key property value and the property. Using the delete keyword, we can remove both the property and its corresponding key from an object.
Demo:
const dog = {
dog_breed: "Husky",
dog_weight: 30,
dog_age: 3,
};
delete dog.dog_weight
console.log(dog);
Output
{ dog_breed: 'Husky', dog_age: 3 }
Explanation
In the above code, we define an object dog that contains three properties. We set the value of the properties and with the help of the delete keyword, we can remove the dog_weight property and value also. When we print the object dog then it only prints the dog_breed and dog_age with their values.
Different ways to Create Objects in JavaScript:
In the process of creating Objects in JavaScript, we can mainly use three methods:
- Using Object Literals
- Creating object with Object.create() method
- By using the constructor function
Using Object Literals:
Object literals is a way to define an Object in JavaScript which also contains key-value pairs within the curly braces.
Demo:
Code:
const user = {
name: "Tech",
age: 12
};
console.log(user.name);
console.log(user.age);
Output:
Tech
12
Explanation
In the above code, we can create a user object with the help of the const keyword that contains two properties name and age. We assign value to the properties and access the value by using the dot with the obj name and property.
Creating object with Object.create() method:
In JavaScript, Object.create() method is used to create a new object with the help of the inheritance concept.
Demo:
const programmer = {
is_learn: false,
printAbout: function(){
console.log(`My first_name is ${this.name}. Am I studying?: ${this.is_learning}`);
}
};
const me = Object.create(programmer);
me.name = 'Smith';
me.is_learning = true;
me.printAbout();
Output:
My first_name is Smith. Am I studying?: true
Explanation
In the above code, we used the inheritance method first we created an object named programmer which has a method named printAbout. By using the object.create(programmer), a new object is created that uses inheritance to inherit the properties from the programmer. The name and is_learning are added directly to the object named me.
By using the constructor function:
Constructors are the methods that are used for initializing the object of the class. The constructor called itself at the time of the object creation. The name of the constructor is the same as the name of the class. With the help of the constructor function, we can create multiple objects.
Demo
Code:
function person(name, gender, age) {
this.name = name;
this.gender = gender;
this.age = age;
}
let man = new person('Adon', 'male', '19.5');
console.log(man.name);
console.log(man.gender);
console.log(man['age']);
Output:
Adon
male
19.5
Explanation
In the above code, we used the constructor function named person that assigns value to the object that has been created. After that we created a new object man by using the new keyword. To access the property of the person, we use both the dot notation and bracket notation.
Conclusion
In conclusion, JavaScript Object methods are useful for working with objects in an easy and efficient way. They provide an easy way to do crucial things for the developer with the help of key and value, modifying data with the use of the object methods. These object methods are useful to perform many operations on data. With the help of object methods, we can improve programs more structured and maintainable way. We can use inheritance to perform operations that help us to improve the functionality of the code. With the help of the object methods, we can perform operations to update the data which is helping for dynamic programming.