var New Array ();
We created an object that is a group type, and array is a native constructor. Obviously, the constructor pattern is to let us know if we are going to create a person (), how can we make it like an array () to create an object instance from a new class, and this object instance is marked as the person type
function person (name, age, job) { Span style= "color: #0000ff;" >this . Name=name; this . Age=age; this . Job=job; this . Sayname = function () {alert ( this .name); };} var person1= new person ("Guoshiwei", 23, "Web" var person2 = new person ("Nicholas", 29, " Software Engineer ");
This code uses the constructor pattern, and when you create an instance from the constructor pattern, you must use the new operator, which is identified by the new operator as the person type (the greatest benefit of the constructor)
Attention:
The first letter of the person should be capitalized, because the constructor mode itself is also a function, in order to distinguish, the function of the construction mode of the first letter capital
The process of creating a new instance through new is as follows (after comment for personal understanding)
- Create a new object;//Open a space in memory object type objects all objects that are not yet defined are object objects
- Assigns the scope of the constructor to the new object (so this is the new object);//Copies a copy of the class or method in the person class into this space, at which point the object becomes the person type object constructor property points to person Indicate which of these properties and methods you are getting from
- Execute the code in the constructor (add a property to the new object);//Put your parameters into the properties or methods of this space
- Returns a new object;//Connect This object to the Person1
So far, the Person1 object instance of a person type is done, but it is noted that the object instance created by the new operator, the second step, I understand is that it is to copy the properties and methods in the person class to themselves, rather than referencing properties and methods from person. (Each property and method is its own)
Alert (Person1.sayname = = person2.sayname); // false
This creates a kind of inconvenience (which I understand to be wasteful), which is to regenerate a sayname function every time an instance is created.
function Person (name, age, Job) { this. name=name; this. age= age; this. job=job; this. sayname=sayname;
}function sayname () { alert (this. name);} var person1=New person ("Guoshiwei", "page", "Web"); var person1=New person ("Nicholas", "Software Engineer");
You can create the Sayname function outside of the function so that both Person1 and Person2 refer to the Sayname () method through the Sayname object, but if you still need sayage, the Sayjob function is more troublesome, and once you need more methods, is not worth the candle, and reduces the encapsulation of the person function, so in order to solve this problem, there is a new mode- prototype mode
javascript--constructor Mode