--Create objects
1: Factory mode: The process of creating an object is wrapped with a function and then invoked on the function to create an instance object
function Createperson (name,age,job) { varnew Object (); = name; = Age ; = job; function () { alert (this. Name); }; return o;} var person1 = Createperson ("Waiting_h", "a", "student"); var = Createperson ("William", "Person2", "star");
View Code
function Person (name,age,job) { this. Name = name; this. Age = Age ; this. Job = job; This function () { alert (this. name);} ;} var New Person ("Waiting_h", "a", "student"); var New Person ("William", "N", "star");
View Code
Of course, in order to avoid the function inside the constructor to be created every time the instantiation is made (the function is also an object), you can define the intrinsic function and use the method of assignment internally to refer to it, so that the attribute in multiple instance objects points to the same function. Instead of being created every time, each instance object has a copy of the function for that property.
At this point, for the constructor mode, we should look at the new operator :
--new operator: Using the new operator goes through the following steps:
--Create a new object;
--assigns the scope of the constructor to the new object (that is, this points to the new object);
--Executes the code in the constructor (adding attributes to the new object);
--Returns the new object.
3: Prototype mode:
= "Waiting_h"="Student"function() { alert (this). name);}; var New Person ();
View Code
——— Prototype Object : Contains properties and methods that can be shared by all instances of a particular type.
When we create a function, we automatically generate a prototype property of the function, which points to the prototype object, which can add properties of the prototype object through the property of the constructor, and the prototype object automatically generates a constructor property while it is created. This property points to the function where the original prototype property resides.
In addition, when we call the constructor to create an instance object, the instance object contains an attribute [[[Prototype]] (ECMAScript 5) that is used to point to the constructor's prototype object .
So: There is no direct relationship between the instance object and the constructor.
Of course, the stereotype attribute has a big drawback--the instance property created by the prototype pattern has the same property method, and for this disadvantage, we can use the instance property to add our own unique property, even if the same name as the property in the prototype object, it will also mask the properties in the prototype object, because when looking for an instance property, Is from the instance itself, if not found, will go to the prototype object to find.
In general, we prefer to use the constructor pattern and prototype mode in conjunction: The constructor pattern is used to define the instance properties, and the prototype pattern is used to define the methods and shared properties.
function Person (name,age,job) { this. Name = name; this. Age = Age ; this. Job = job; this. Friends = ["Biglun", "William"= { Constructor:person; function () { alert (this. Name); }} var New Person ("Waiting_h", "student");
View Code
Object 2--"JavaScript Advanced Program Design" Reading notes