Skip to content

Commit 144e761

Browse files
committed
Update ways to create an object question
1 parent 2e84391 commit 144e761

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@
542542

543543
Object literal property values can be of any data type, including array, function, and nested object.
544544

545-
**Note:** This is one of the easiest ways to create an object.
545+
**Note:** This is one of the easiest ways to create an object and it's most commonly used for creating simple, ad-hoc objects.
546546

547547
2. **Object constructor:**
548548

@@ -552,16 +552,16 @@
552552
var object = new Object();
553553
```
554554

555-
The `Object()` is a built-in constructor function so "new" keyword is not required. The above code snippet can be re-written as:
555+
The `Object()` is a built-in constructor function so "new" keyword is not required for creating plain objects. The above code snippet can be re-written as:
556556

557557
```javascript
558558
var object = Object();
559559
```
560-
560+
However, `Object()` can be used to either create a plain object or convert a given value into its corresponding object wrapper, whereas `new Object()` is specifically used to explicitly create a new object instance.
561+
561562
3. **Object's create method:**
562563

563-
The `create` method of Object is used to create a new object by passing the specified prototype object and properties as arguments, i.e., this pattern is helpful to create new objects based on existing objects.
564-
The second argument is optional and it is used to create properties on a newly created object.
564+
The `create` method of Object is used to create a new object by passing the specified prototype object and properties as arguments, i.e., this pattern is helpful to create new objects based on existing objects. In other words, this is useful for setting up **prototypal inheritance**. The second argument is optional and it is used to create properties on a newly created object.
565565

566566
The following code creates a new empty object whose prototype is null.
567567

@@ -592,7 +592,7 @@
592592

593593
4. **Function constructor:**
594594

595-
In this approach, create any function and apply the new operator to create object instances.
595+
In this approach, create any function and apply the new operator to create object instances. This was the main way to do constructor-based OOP before ES6 classes.
596596

597597
```javascript
598598
function Person(name) {
@@ -604,38 +604,47 @@
604604

605605
5. **Function constructor with prototype:**
606606

607-
This is similar to function constructor but it uses prototype for their properties and methods,
607+
This is similar to function constructor but it uses prototype for their properties and methods. Using prototype means you're sharing methods/properties across instances, which saves memory and improve performance.
608608

609609
```javascript
610610
function Person() {}
611611
Person.prototype.name = "Sudheer";
612612
var object = new Person();
613613
```
614614

615-
This is equivalent to creating an instance with Object.create method with a function prototype and then calling that function with an instance and parameters as arguments.
615+
This is equivalent to creating an instance with `Object.create` method with a function prototype and then calling that function with an instance and parameters as arguments.
616616

617617
```javascript
618-
function func() {}
618+
function func(x, y, z) {
619+
this.x = x;
620+
this.y = y;
621+
this.z = z;
622+
}
619623

620-
new func(x, y, z);
624+
var instance = new func(1, 2, 3);
621625
```
622626

623627
**(OR)**
624628

625629
```javascript
630+
function func(x, y, z) {
631+
this.x = x;
632+
this.y = y;
633+
this.z = z;
634+
}
626635
// Create a new instance using function prototype.
627636
var newInstance = Object.create(func.prototype)
628637
629638
// Call the function
630-
var result = func.call(newInstance, x, y, z),
639+
var result = func.call(newInstance, 1, 2, 3),
631640
632641
// If the result is a non-null object then use it otherwise just use the new instance.
633642
console.log(result && typeof result === 'object' ? result : newInstance);
634643
```
635644

636645
6. **Object's assign method:**
637646
638-
The `Object.assign` method is used to copy all the properties from one or more source objects and stores them into a target object.
647+
The `Object.assign` method is used to copy all the properties from one or more source objects and stores them into a target object. This is mainly used for cloning and merging
639648
640649
The following code creates a new staff object by copying properties of his working company and the car he owns.
641650
@@ -647,7 +656,7 @@
647656
648657
7. **ES6 Class syntax:**
649658
650-
ES6 introduces class feature to create objects.
659+
ES6 introduces class feature to create objects. This is syntactic sugar over the prototype-based system.
651660
652661
```javascript
653662
class Person {
@@ -669,6 +678,8 @@
669678
})();
670679
```
671680

681+
In modern apps, Singletons are often implemented using **modules** or **closures**.
682+
672683
**[⬆ Back to Top](#table-of-contents)**
673684

674685
2. ### What is a prototype chain

0 commit comments

Comments
 (0)