Home >Web Front-end >JS Tutorial >Basic knowledge about overloading and factory methods of JS constructors
is written in front
Sometimes we want to initialize objects in multiple ways. For example, initialize a Set object through an array of elements instead of initializing it by passing in the parameter list of the constructor
By overloading this constructor method, it can execute different initialization methods according to the different parameters passed in.
The Set() constructor defined in this code can explicitly pass in a set of elements as a parameter list, or it can pass in an array of elements. But this constructor is ambiguous. If a parameter of the collection is an array, the collection cannot be created through this constructor (in order to do this, you need to first create an empty collection and then explicitly call add() method).
A class method is used to return an instance of the class.
Factory methods with different names are used to perform different initializations. But since the constructor is the public identifier of the class, each class can only have one constructor. But this is not a rule that must be followed.
Initialize this new object by calling Set() as a function
Nothing is impossible with JavaScript, only unexpected