OBJECT
JavaScript objects are created using the notation, also known as object literal notation. For example, the following code creates an object that represents a person:
Objects in JavaScript can also be created using a constructor function, which is a special kind of function that is used to create and initialize new objects. For example, the following code defines a constructor function Person that can be used to create new Person objects:
1function Person(name, age) {
2 this.name = name;
3 this.age = age;
4 this.greet = function() {
5 return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
6 }
7}
8
9let person1 = new Person("John", 30);
10let person2 = new Person("Jane", 25);
In addition, JavaScript also provides a Object() constructor function that can be used to create an empty object with no properties.
Conclusion
Objects in JavaScript are a fundamental concept that are used to represent real-world entities. They can be created using object literal notation or constructor functions, and they have properties and methods that can be accessed and manipulated. Understanding how to work with objects in JavaScript is essential for creating robust and maintainable code, and also it's the foundation of OOP in JavaScript.