You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/javascript/object-constructor.md
+43-42Lines changed: 43 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
id: Object Constructor
2
+
id: 0bject Constructor
3
3
title: JavaScript Object Constructor
4
4
sidebar_label: Object Constructor
5
5
sidebar_position: 17
@@ -136,17 +136,19 @@ A constructor is a function that initializes an object. In JavaScript, construct
136
136
137
137
In JavaScript, there is a special constructor function known as `Object()` that is used to create and initialize an object. The return value of the `Object()` constructor is assigned to a variable. The variable contains a reference to the new object. We need an object constructor to create an object “type” that can be used multiple times without redefining the object every time.
138
138
139
-
#### Example
139
+
#Example
140
140
141
141
```javascript
142
-
functionGFG(A, B, C) {
143
-
this.g=A;
144
-
this.f=B;
145
-
this.gg=C;
142
+
classCar {
143
+
constructor(brand, model, year) {
144
+
this.brand= brand;
145
+
this.model= model;
146
+
this.year= year;
147
+
}
146
148
}
147
149
```
148
-
Here, `GFG` is the constructor name and `A`, `B`, `C` are the arguments of the constructor.
149
150
151
+
In this example, `Car` is the class name, and `brand`, `model`, and `year` are the parameters of the constructor.
150
152
### Instantiating an Object Constructor
151
153
152
154
There are two ways to instantiate an object constructor:
@@ -170,112 +172,111 @@ There are two ways to assign properties to objects:
170
172
#### Using Dot (.) Operator
171
173
172
174
```javascript
173
-
object_name.properties = value;
175
+
objectName.propertyName = value;
174
176
```
175
177
176
178
#### Using Bracket Notation
177
179
178
180
```javascript
179
-
object_name['properties'] = value;
181
+
objectName['propertyName'] = value;
180
182
```
181
183
182
184
#### Example 1: Using `new` Keyword and Dot Operator
183
185
184
186
```javascript
185
187
// Creating object using "new" keyword
186
-
var gfg = new Object();
188
+
var myObject = new Object();
187
189
188
190
// Assigning properties to the object using dot (.) operator
189
-
gfg.a = "JavaScript";
190
-
gfg.b = "GeeksforGeeks";
191
+
myObject.subject = "JavaScript";
192
+
myObject.author = "John Doe";
191
193
192
-
console.log("Subject: " + gfg.a);
193
-
console.log("Author: " + gfg.b );
194
+
console.log("Subject: " + myObject.subject);
195
+
console.log("Author: " + myObject.author);
194
196
```
195
197
**Output:**
196
198
```
197
199
Subject: JavaScript
198
-
Author: GeeksforGeeks
200
+
Author: John Doe
199
201
```
200
202
201
203
#### Example 2: Using Curly Braces and Bracket Notation
202
204
203
205
```javascript
204
206
// Creating an object using "{ }" bracket
205
-
var gfg = { };
207
+
var myObject = { };
206
208
207
209
// Assigning properties to the object using bracket notation
208
-
gfg['a'] ="JavaScript";
209
-
gfg['b'] ="GeeksforGeeks";
210
+
myObject['subject'] ="JavaScript";
211
+
myObject['author'] ="Jane Smith";
210
212
211
-
console.log("Subject: "+gfg.a);
212
-
console.log("Author: "+gfg.b);
213
+
console.log("Subject: "+myObject.subject);
214
+
console.log("Author: "+myObject.author);
213
215
```
214
216
**Output:**
215
217
```
216
218
Subject: JavaScript
217
-
Author: GeeksforGeeks
219
+
Author: Jane Smith
218
220
```
219
221
220
222
#### Example 3: Using Function with Object Constructor
221
223
222
224
```javascript
223
225
// Creating object
224
-
vargfg=newObject();
226
+
varmyObject=newObject();
225
227
226
228
// Assigning properties to the object
227
-
gfg.a="JavaScript";
228
-
gfg.b="GeeksforGeeks";
229
+
myObject.subject="JavaScript";
230
+
myObject.author="James Brown";
229
231
230
232
// Use function()
231
-
gfg.c=function () {
232
-
return (gfg.a+""+gfg.b);
233
+
myObject.displayInfo=function () {
234
+
return (myObject.subject+""+myObject.author);
233
235
};
234
236
235
-
console.log("Subject: "+gfg.a);
236
-
console.log("Author: "+gfg.b);
237
+
console.log("Subject: "+myObject.subject);
238
+
console.log("Author: "+myObject.author);
237
239
238
240
// Call function with object constructor
239
-
console.log("Adding the strings: "+gfg.c() );
241
+
console.log("Adding the strings: "+myObject.displayInfo());
240
242
```
241
243
**Output:**
242
244
```
243
245
Subject: JavaScript
244
-
Author: GeeksforGeeks
245
-
Adding the strings: JavaScript GeeksforGeeks
246
+
Author: James Brown
247
+
Adding the strings: JavaScript James Brown
246
248
```
247
249
248
250
#### Example: Another Way to Create a Function Using Function Name
249
251
250
252
```javascript
251
253
// Creating object using "{ }" bracket
252
-
vargfg= { };
254
+
varmyObject= { };
253
255
254
256
// Assigning properties to the object
255
-
gfg.a="JavaScript";
256
-
gfg.b="GeeksforGeeks";
257
+
myObject.subject="JavaScript";
258
+
myObject.author="Jessica Green";
257
259
258
260
// Use function()
259
-
gfg.c= add;
261
+
myObject.displayInfo= add;
260
262
261
263
// Declare function add()
262
264
functionadd() {
263
-
return (gfg.a+""+gfg.b);
265
+
return (myObject.subject+""+myObject.author);
264
266
};
265
267
266
-
console.log("Subject: "+gfg.a);
267
-
console.log("Author: "+gfg.b);
268
+
console.log("Subject: "+myObject.subject);
269
+
console.log("Author: "+myObject.author);
268
270
269
271
// Call function with object constructor
270
-
console.log("Adding the strings: "+gfg.c());
272
+
console.log("Adding the strings: "+myObject.displayInfo());
271
273
```
272
274
**Output:**
273
275
```
274
276
Subject: JavaScript
275
-
Author: GeeksforGeeks
276
-
Adding the strings: JavaScript GeeksforGeeks
277
+
Author: Jessica Green
278
+
Adding the strings: JavaScript Jessica Green
277
279
```
278
-
279
280
## Conclusion
280
281
281
282
Object constructor functions in JavaScript provide a powerful way to create and manage objects of the same type. By following best practices, such as using prototypes and understanding the limitations of constructors, you can write more efficient and maintainable code.
0 commit comments