Factory methods and constructors to create objects
Why use factory methods and constructors
It is impossible for us to use literals to create a large number of objects, where there are many duplicate codes, we need a factory to mass produce similar objects.
Factory method
Use the factory method to create objects, and the constructors used are all Object, so the created objects are all of the Object type, which makes us unable to distinguish different types of objectsJust don't know where it came from
Constructor
Create a custom object, capitalize the first letter, call ordinary functions directly, and use the new keyword for the constructor.
Constructor execution flow:
1. Create a new object immediately;
2. Assign the scope of the constructor to the new object (that is, this points to the new object);
3. Execute the code in the constructor line by line;
4. Return the new one Object.
Using the same constructor to create an object is the same type of object, the constructor is also called a class, and the object created by the constructor is called an instance of the class
Syntax: object instanceof class (constructor) Determine whether the object is an instance of the class.
All objects are instances of Object.