Initialize an object in JavaScript
This post will discuss how to initialize an object in JavaScript.
There are different ways to initialize an object in JavaScript. An object is a collection of properties and functions that can be used to store and manipulate data. Here are some of the methods we can use:
1. Using an object literal
This is a simple and concise way to create objects with properties and values inside curly braces. We use curly braces {}
to enclose a comma-separated list of property names and values. The property names can be either identifiers or strings, and the values can be any valid JavaScript expression. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Create an object with three properties: name, age, and fun let person = { name: "Evelyn", age: 20, fun: function() { console.log("Hello, my name is " + this.name); } }; // Access the properties using dot notation or bracket notation console.log(person.name); // Evelyn console.log(person["age"]); // 20 // Invoke the function using dot notation person.fun(); // Hello, my name is Evelyn |
2. Using new operator and a constructor function
A constructor function is a custom function that defines the properties and functions of an object, and can be used to create an object. The constructor function acts as a template for the object. It can have parameters that initialize the properties of the object, and use the this
keyword to refer to the current object. We can use the new
operator to create a new instance of the object. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Define a function that acts as a constructor for the person object function Person(name, age) { this.name = name; this.age = age; this.fun = function() { console.log("Hello, my name is " + this.name); }; } // Create a new instance of the person object using the new operator let person = new Person("Evelyn", 20); // Access the properties using dot notation or bracket notation console.log(person.name); // Evelyn console.log(person["age"]); // 20 // Invoke the function using dot notation person.fun(); // Hello, my name is Evelyn |
3. Using Object.create()
function
This is a more advanced way to create an object. We use the Object.create() function to create a new object that inherits from another object. The first argument of the function is the prototype object that we want to inherit from, and the second argument is an optional object that defines additional properties for the new object. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Create an object that acts as a prototype for the person object let personProto = { fun: function() { console.log("Hello, my name is " + this.name); } }; // Create a new instance of the person object using Object.create let person = Object.create(personProto, { name: { value: "Evelyn" }, age: { value: 20 } }); // Access the properties using dot notation or bracket notation console.log(person.name); // Evelyn console.log(person["age"]); // 20 // Invoke the function using dot notation person.fun(); // Hello, my name is Evelyn |
4. Using new Object()
constructor function
This is a built-in constructor function that creates an empty object. We can then add properties and values to the object using dot notation or bracket notation. For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create an object with three properties: name, age, and fun let person = new Object(); person.name = "Evelyn"; person["age"] = 20; person.fun = function() { console.log("Hello, my name is " + this.name); }; // Access the properties using dot notation or bracket notation console.log(person.name); // Evelyn console.log(person["age"]); // 20 // Invoke the function using dot notation person.fun(); // Hello, my name is Evelyn |
That’s all about initializing an object in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)