1. Factory mode
Cons: There is no problem solving object recognition, that is, how to know the type of an object.
2. Constructor mode
Compared to the factory model:
1. There is no explicit creation of an object
2. Assign properties and methods directly to the This object
3. No Return statement
To create an instance of the person, you must use the new operator, in which case the constructor is actually going through 4 steps:
1. Create a new object
2. Assigning the scope of a constructor to a new object
3. Execute the code in the constructor
4. Return to new Object
Create a custom constructor to identify its instance as a specific type.
disadvantages of the constructor:
Each method has to be recreated on each instance. Both Person1 and Person2 have a method of sayname (), but two methods are not the same instance of a function. Functions with the same name on different instances are not equal.
It is not necessary to create two function instances to accomplish the same task, and the This object is not required to bind the function to a specific object before executing the code, as follows.
Set the Sayname property to the global sayname function, so that because Sayname contains a pointer to a function, the Person1 and Person2 objects share the same function.
However, if an object needs to define many methods, it is necessary to define many global functions, and the custom reference types are not encapsulated. In order to solve the above problems, the prototype model is introduced.
3. Prototype mode
Understanding prototype Objects
Each function we create has a prototype property, which is a pointer to an object, and the purpose of this object is to include properties and methods that can be shared by all instances of a particular type. Prototype is the object prototype of the object instance created by invoking the constructor, and the advantage of using a prototype object is that all object instances can share the properties and methods that it contains.
First, the parser asks if the instance Person1 has a name attribute, and if so, returns.
If not, continue to search for the Name property in the Person1 prototype, and return if there is one.
If not, continue searching for prototypes of Person1 prototypes.
isprototypeof () determining the association between an instance and a prototype object
Console.log (Person.prototype.isPrototypeOf (Person1)); True
object.getprototypeof () returns the value of [[prototype]]
Console.log (object.getprototypeof (Person1));
person {name: "Yvette", age:26, Job: "Engineer"} returns the prototype object for person.
Console.log (object.getprototypeof (person1) = = = Person.prototype)//true
Console.log (Object.getprototypeof (person1). name);//"Yvette"
The hasOwnProperty () method can detect whether a property exists in an instance or in a prototype, and returns true only if a given property exists in the instance.
Console.log (Person1.hasownproperty ("name"));//false
prototypes and in Operators
There are two ways to use the In operator: use separately and in the for-in loop. When used alone, the in operator returns True when the given property is accessible through the object, whether the property is in an instance or in a prototype.
With the For In loop, all enumerable properties that can be accessed through an object are returned, including both the attributes in the instance and the properties that exist in the prototype. If the attribute in the instance masks a property that is not enumerable in the prototype, it is also returned. There is a bug on the version implementation prior to IE9, and the instance properties that mask the non-enumerable properties are not returned in for-in.
Before IE9, there is no log information in this section. Although the ToString () method in the person instance masks the non-enumerable ToString () in the prototype;
prototype Shorthand
This causes the Person1.constructor to no longer point to person, but to object. If constructor is important, you need to deliberately set it to the appropriate value, such as:
However, this approach causes the constructor property to become enumerable.
If you want to set to non-enumerable (the default is not enumerable), you can use Object.defineproperty (Person.prototype, "constructor", {
Enumerable:false,
Value:person
});
The dynamic nature of prototypes
Because the process of looking up values in a prototype is a search, any modifications we make to the prototype object are immediately reflected from the instance.
If you rewrite the entire prototype object, the situation is different. When the constructor is called, a [[prototype]] pointer is added to the instance that points to the original prototype, and modifying the prototype to another object is tantamount to cutting off the connection between the constructor and the original prototype. The pointer in the instance only points to the prototype, not to the constructor.
Person.prototype points to the original prototype object, not to the new prototype object.
problems with prototype objects
The biggest problem with prototype mode is caused by the nature of its sharing.
For attributes that contain reference type values, the problem is more prominent
The intention is only to modify the friends of Person1, but the Friends attribute value of Person2 is changed. So we seldom use prototype mode alone.
4. Combination of construction mode and prototype mode
The most common way to create a custom type is to combine the constructor pattern with the prototype pattern. The constructor pattern is used to define instance properties, which are used to define methods and shared properties, so that each instance has its own copy of the instance properties, while also sharing a reference to the method, maximizing memory savings.
In addition to the above methods, there are also dynamic prototype mode, parasitic tectonic mode and the safe structure mode, but given the low frequency of use, no longer repeat.
JS create objects in several different ways