Skip to content

Commit b5db7c0

Browse files
committed
chnaged examples
1 parent a951459 commit b5db7c0

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

docs/javascript/object-constructor.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
id: Object Constructor
2+
id: 0bject Constructor
33
title: JavaScript Object Constructor
44
sidebar_label: Object Constructor
55
sidebar_position: 17
@@ -136,17 +136,19 @@ A constructor is a function that initializes an object. In JavaScript, construct
136136

137137
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.
138138

139-
#### Example
139+
#Example
140140

141141
```javascript
142-
function GFG(A, B, C) {
143-
this.g = A;
144-
this.f = B;
145-
this.gg = C;
142+
class Car {
143+
constructor(brand, model, year) {
144+
this.brand = brand;
145+
this.model = model;
146+
this.year = year;
147+
}
146148
}
147149
```
148-
Here, `GFG` is the constructor name and `A`, `B`, `C` are the arguments of the constructor.
149150

151+
In this example, `Car` is the class name, and `brand`, `model`, and `year` are the parameters of the constructor.
150152
### Instantiating an Object Constructor
151153

152154
There are two ways to instantiate an object constructor:
@@ -170,112 +172,111 @@ There are two ways to assign properties to objects:
170172
#### Using Dot (.) Operator
171173

172174
```javascript
173-
object_name.properties = value;
175+
objectName.propertyName = value;
174176
```
175177

176178
#### Using Bracket Notation
177179

178180
```javascript
179-
object_name['properties'] = value;
181+
objectName['propertyName'] = value;
180182
```
181183

182184
#### Example 1: Using `new` Keyword and Dot Operator
183185

184186
```javascript
185187
// Creating object using "new" keyword
186-
var gfg = new Object();
188+
var myObject = new Object();
187189

188190
// 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";
191193

192-
console.log("Subject: " + gfg.a);
193-
console.log("Author: " + gfg.b );
194+
console.log("Subject: " + myObject.subject);
195+
console.log("Author: " + myObject.author);
194196
```
195197
**Output:**
196198
```
197199
Subject: JavaScript
198-
Author: GeeksforGeeks
200+
Author: John Doe
199201
```
200202

201203
#### Example 2: Using Curly Braces and Bracket Notation
202204

203205
```javascript
204206
// Creating an object using "{ }" bracket
205-
var gfg = { };
207+
var myObject = { };
206208

207209
// 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";
210212

211-
console.log("Subject: " + gfg.a);
212-
console.log("Author: " + gfg.b );
213+
console.log("Subject: " + myObject.subject);
214+
console.log("Author: " + myObject.author);
213215
```
214216
**Output:**
215217
```
216218
Subject: JavaScript
217-
Author: GeeksforGeeks
219+
Author: Jane Smith
218220
```
219221

220222
#### Example 3: Using Function with Object Constructor
221223

222224
```javascript
223225
// Creating object
224-
var gfg = new Object();
226+
var myObject = new Object();
225227

226228
// Assigning properties to the object
227-
gfg.a = "JavaScript";
228-
gfg.b = "GeeksforGeeks";
229+
myObject.subject = "JavaScript";
230+
myObject.author = "James Brown";
229231

230232
// Use function()
231-
gfg.c = function () {
232-
return (gfg.a + " " + gfg.b);
233+
myObject.displayInfo = function () {
234+
return (myObject.subject + " " + myObject.author);
233235
};
234236

235-
console.log("Subject: " + gfg.a);
236-
console.log("Author: " + gfg.b);
237+
console.log("Subject: " + myObject.subject);
238+
console.log("Author: " + myObject.author);
237239

238240
// Call function with object constructor
239-
console.log("Adding the strings: " + gfg.c() );
241+
console.log("Adding the strings: " + myObject.displayInfo());
240242
```
241243
**Output:**
242244
```
243245
Subject: JavaScript
244-
Author: GeeksforGeeks
245-
Adding the strings: JavaScript GeeksforGeeks
246+
Author: James Brown
247+
Adding the strings: JavaScript James Brown
246248
```
247249

248250
#### Example: Another Way to Create a Function Using Function Name
249251

250252
```javascript
251253
// Creating object using "{ }" bracket
252-
var gfg = { };
254+
var myObject = { };
253255

254256
// Assigning properties to the object
255-
gfg.a = "JavaScript";
256-
gfg.b = "GeeksforGeeks";
257+
myObject.subject = "JavaScript";
258+
myObject.author = "Jessica Green";
257259

258260
// Use function()
259-
gfg.c = add;
261+
myObject.displayInfo = add;
260262

261263
// Declare function add()
262264
function add() {
263-
return (gfg.a + " " + gfg.b);
265+
return (myObject.subject + " " + myObject.author);
264266
};
265267

266-
console.log("Subject: " + gfg.a);
267-
console.log("Author: " + gfg.b);
268+
console.log("Subject: " + myObject.subject);
269+
console.log("Author: " + myObject.author);
268270

269271
// Call function with object constructor
270-
console.log("Adding the strings: " + gfg.c());
272+
console.log("Adding the strings: " + myObject.displayInfo());
271273
```
272274
**Output:**
273275
```
274276
Subject: JavaScript
275-
Author: GeeksforGeeks
276-
Adding the strings: JavaScript GeeksforGeeks
277+
Author: Jessica Green
278+
Adding the strings: JavaScript Jessica Green
277279
```
278-
279280
## Conclusion
280281

281282
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

Comments
 (0)