JavaScript Object Constructors (original) (raw)
Last Updated : 18 Jan, 2025
An object is the collection of related data or functionality in the form of **key. These functionalities usually consist of several functions and variables. All JavaScript values are objects except primitives.
const GFG = {
subject : "programming",
language : "JavaScript",
}
Here, _subject and _language are the **keys and _programming and _JavaScript are the **values.
Object Constructor
A constructor function is a special type of function in JavaScript that is used to create and initialize objects. Constructors are used with the new keyword to create instances of a particular type (object). By using constructors, we can easily create multiple instances of the same type of object, all with their own unique properties and methods.
JavaScript ``
//Driver Code Starts{ // Constructor function //Driver Code Ends }
function Person(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
console.log(My name is <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mi>a</mi><mi>n</mi><mi>d</mi><mi>I</mi><mi>a</mi><mi>m</mi></mrow><annotation encoding="application/x-tex">{this.name} and I am </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mord mathnormal">an</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">am</span></span></span></span>{this.age} years old.
);
};
}
//Driver Code Starts{
//Creating Instances with a Constructor const p1 = new Person("Akash", 30); const p2 = new Person("Anvesh", 25);
p1.sayHello(); p2.sayHello(); //Driver Code Ends }
``
Output
My name is Akash and I am 30 years old. My name is Anvesh and I am 25 years old.
**this keyword
The **this keyword refers to the object it belongs to, like OOPs languages C++, C#, JAVA etc. **this keyword is used in different ways in different areas. While executing a function in JavaScript that has a reference to its current execution context, that is the reference by which the function or data member is called. See the previous example.
**Adding Property to an Object
The property can be added to the object by using **dot(.) operator or **square bracket.,
const GFG = {
articles: 'computer science',
quantity: 3000,
};
The **GFG has two properties “articles” and “quantity”. Now we wish to add one more property name called **subject.
Using dot (.) operator
GFG.subject: 'JavaScript';
Using square bracket:
GFG['subject']: 'JavaScript';
Here, **subject is the property and **‘JavaScript’ is the value of the property. **Adding a property to Constructor: We cannot add a property to an existing constructor like adding a property to an object (see previous point), for adding a property we need to declare under the constructor.
function GFG(a, b, c) {
this.A = a;
this.B = b;
this.C = c;
this.G = "GEEK";
}
Here, we add a property name **G with value **“GEEK”, in this case the value **“GEEK” is not passed as an argument.
**Adding a Method to an Object
We can add a new method to an existing object.
GFG.n = function () {
return this.A + this.B;
};
Here, the object is GFG.
**Adding a Method to Constructor
function GFG(a, b, c) {
this.A = a;
this.B = b;
this.C = c;
this.n = function () {
return this.A + this.B;
}
}
Here, in the last line a method is added to an object.
**Instantiating an object constructor
There are two ways to instantiate object constructor,
const object_name = new Object(); // or
const object_name = new Object("java", "JavaScript", "C#");
const object_name = { };
In 1st method, the object is created by using **new keyword like normal OOP languages, and **“Java”, “JavaScript”, “C#” are the arguments, that are passed when the constructor is invoked. In 2nd method, the object is created by using **curly braces “{ }”.
**Assigning properties to the objects: There are two ways to assigning properties to the objects.
- **Using dot (.) operator:
object_name . properties = value;
- **Using third bracket:
object_name [ 'properties'] = value;
**Example 1: Object creation by using **new keyword and assigning properties to the object using **dot(.) operator.
javascript `
let gfg = new Object();
gfg.a = "JavaScript"; gfg.b = "GeeksforGeeks";
//Driver Code Starts{ console.log("Subject: " + gfg.a); console.log("Author: " + gfg.b ); //Driver Code Ends }
`
Output
Subject: JavaScript Author: GeeksforGeeks
**Example 2: Object creation using **curly braces and assigning properties to the object using **third bracket “[]” operator.
javascript `
let gfg = { };
gfg['a'] = "JavaScript"; gfg['b']= "GeeksforGeeks";
//Driver Code Starts{ console.log("Subject: " + gfg.a); console.log("Author: " + gfg.b ); //Driver Code Ends }
`
Output
Subject: JavaScript Author: GeeksforGeeks
**Example 3: This example shows how to use function() with object constructor.
javascript `
let gfg = new Object();
gfg.a = "JavaScript"; gfg.b = "GeeksforGeeks"; gfg.c = function () { return (gfg.a +" "+ gfg.b); };
//Driver Code Starts{ console.log("Subject: " + gfg.a); console.log("Author: " + gfg.b); console.log("Adding the strings: "+ gfg.c() ); //Driver Code Ends }
`
Output
Subject: JavaScript Author: GeeksforGeeks Adding the strings: JavaScript GeeksforGeeks
**Example 4: Another way to create a function using function name.
javascript `
let gfg = { };
gfg.a = "JavaScript"; gfg.b = "GeeksforGeeks"; gfg.c = add;
// Declare function add() function add() { return (gfg.a +" "+ gfg.b); };
//Driver Code Starts{ console.log("Subject: " + gfg.a); console.log("Author: " + gfg.b); console.log("Adding the strings: "+ gfg.c()); //Driver Code Ends }
`
Output
Subject: JavaScript Author: GeeksforGeeks Adding the strings: JavaScript GeeksforGeeks