If there will be only one instance of the object, should I use a constructor?

I traveled deeper into the JS world and came across 3 different ways that I could develop an external website basket:

Constructor with prototype functions

var cart = function(){ this.items = {} } cart.prototype.increaseItemQty = function(partNumber){ if(this.items[partNumber]){ this.items[partNumber].qty += 1; } else { this.items[partNumber] = { qty : 1 }; } } cart = new cart(); 

Methods inside the constructor

 var cart2 = function(){ this.items = {}; this.increaseItemQty = function (partNumber) { if(this.items[partNumber]){ this.items[partNumber].qty += 1; } else { this.items[partNumber] = { qty : 1 }; } } } cart2 = new cart2(); 

Object Methods

 var cart3 = { items : {}, increaseItemQty : function(partNumber){ if(this.items[partNumber]){ this.items[partNumber].qty += 1; } else { this.items[partNumber] = { qty : 1 }; } } }; 

If I know that there will be only one instance of the cart, should I just use the Object Method ? Is there a reason why I should use the constructor?

+5
source share
4 answers

Yes, there is a (minor) reason why you should not use the constructor: the instance will contain a reference to the constructor function through instance.[[Prototype]].constructor , and thus it will not be garbage collection. It will be useless to destroy memory, because you will not create it again.

 var cart = new function(){ /* ... */ }; console.log(cart.constructor); // Won't be garbage-collected 

If you prefer a functional approach, you can still do something like the following. This way you can also have private variables.

 var cart = function() { // You can add private variables here this.items = {}; this.increaseItemQty = function(partNumber) { if(this.items[partNumber]) { this.items[partNumber].qty += 1; } else { this.items[partNumber] = { qty : 1 }; } }; return this; }.call({}); cart.increaseItemQty(0); console.log(cart.items); 
+3
source

You should use the constructor functions if you think that you will need to initialize your carts with some default values ​​or make some initialization elements, which must be performed before using the entire object.

On the other hand, maybe you are missing Object.create . You do not need constructor functions to implement the prototype chain:

 var A = { doStuff() { console.log("I did some stuff"); } }; var B = Object.create(A); B.moreStuff = function() { console.log("I did even more stuff"); }; // A new object where its prototype is B var someB = Object.create(B); b.doStuff(); // ...from A b.moreStuff(); // ...from B 
+2
source

The difference between cases 1 and 2: in case 1 you assign methods to all instances of the cart class or its child classes, in case 2 only to instances of cart .

The difference between cases 2 and 3: in case 3, in addition, you create only one instance, simply using the object initializer expression and assigning some methods as your properties.

Now, if you answer your question, if you have only one instance, theoretically it would be enough for you to add a method as its property, as in case 3. Or even write procedural style functions.

But in terms of extensibility and code reuse, it's best to use methods when creating classes . Classes in javascript are collections of objects with the same prototype. So, it is best to add methods as properties of the prototype object, for example, in case 1 . If you have only one copy - you will not make any difference. But since cart should be a rather heavy and complex class, whenever the requirements for your basket change, case 1 will be the most convenient.

+1
source

To create an object, in principle, you can create it using the "constructor" function or simply with the object name "{}", which both do exactly the same thing in your case above.

The main advantage of a constructor template is that it will be more useful if you need to deal with arguments when creating an instance, as shown below.

 function Car(model) { this.model = model; } 

Or, if you need to use private variables and methods, as shown below.

 function Car() { // private variable var modelYear = 1990; // private method var isEligibleToInsurance = function() { return this.modelYear > modelYear; }; this.isLatestModel = function() { return isEligibleToInsurance(); }; } 

This way you can have a private method and use it inside public methods. When you use object notation, you cannot have a common private method used in all public methods, and when you use 'prototype', you cannot use private methods declared inside the constructor.

Note When you name the class, you can follow the pascal example to name it "Car" instead of "car" and create instances with a camel case as "var car = new Car ();"

+1
source

Source: https://habr.com/ru/post/1261471/


All Articles