<pre name="code" class="javascript">function createPerson(name,age){
  var o=new Object();
  o.name=name;
  o.age=age;
  o.sayName=function(){
    alert(this.name);
  };
  return o;
}
 
var person1=createPerson("name",32);
var person2=createPerson("name2",34);
Copy the code

The factory pattern abstracts the process of creating a concrete object, using functions to encapsulate the details of creating an object with a specific interface. But it does not solve the object recognition problem.

function Person(name,age){
  this.name=name;
  this.age=age;
  this.sayName=function(){
    alert(this.name);
  };
}
Copy the code

var person1=new Person(“name”,32); var person2=new Person(“name2”,34); Constructor mode creates an object without showing, assigns properties and methods directly to this object, and has no return statement. To create a new instance of Person, you must use the new keyword, and the constructor actually goes through four steps: create a new object, assign the scope of the constructor to the new object (so this points to it), execute the constructor’s code (add properties to the new object), and return the new object. At the end of the previous example, Person1 and Person2 each hold a different instance of Person. Both of these objects have a constructor property that points to Person, such as:


alert(person1.constructor==Person); //true

alert(person2.constructor==Person); //true
Copy the code

The Object we create in this example is both an instance of Object and an instance of Person. Such as:

alert(person1 instanceof Object); //true alert(person1 instanceof Person); //true alert(person2 instanceof Object); //true alert(person2 instanceof Person); //trueCopy the code

Creating a custom constructor means that future instances of it can be identified as a particular type.

Any function, called with new, can be a constructor.

The problem with constructors is that every time you define a function, you instantiate an object. Logically, the constructor can be defined as follows:

function Person(name,age){ this.name=name; this.age=age; this.sayName=new Function("alert(this.name);" ); }Copy the code

Looking at the constructor from this perspective, it’s easier to see the nature of each Person instance containing a different Function instance. Creating a Function in this way results in different scope chains and identifier parsing, but the mechanism for creating a Function instance is the same, so functions with the same name on different instances are not equal. alert(person1.sayName==person2.sayName); //false

But it’s not really necessary to create two instances of Function that do the same thing, and with this object there’s no need to bind the Function to a specific object before executing the code.

function Person(name,age){ this.name=name; this.age=age; this.sayName=sayName; } function sayName{ alert(this.name); } var person1=new Person("name",32); var person2=new Person("name2",34); function Person(){ } Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function(){ alert(this.name); }; // Write an alert(person.name); Var person1=new Person(); person1.sayName(); / / "Nicholas" var person2 = newPerson (); person2.sayName(); //"Nicholas" alert(person1.sayName==person2.sayName); //trueCopy the code

Each function we create has a Prototype property, which is a pointer to an object whose purpose is to contain properties and methods that can be shared by all instances of a particular type. A prototype is an object that is the prototype of the object created by calling the constructor. The advantage of using prototype objects is that all object instances share the properties and methods it contains. Instead of defining the object instance information in the constructor, you can add this information directly to the prototype object. Here, we add the sayName() method and all the properties directly to the Person’s Prototype property, and the constructor becomes empty. Even so, you can still create a new object by calling the constructor, and the new object will have the same properties and methods. But unlike the constructor pattern, these properties and methods of the new object are shared by all instances.

Using object.getPrototypeof () makes it easy to get a prototype of an Object, which is important when using prototypes to implement inheritance.

Every time the code reads a property of each object, a search is performed for the property with the given name. The search begins with the object instance itself. If an attribute with the given name is found in the instance, the value of that attribute is returned. If it is not found, the search continues to the stereotype object to which the pointer points, looking for the attribute with the given name in the stereotype object. If the property is found in the stereotype object, the value of the property is returned.

person1.name="Greg"; alert(person1.name); //Greg-- from instance alert(person2.name); //Nicholas-- from the stereotype Delete the instance property completely delete person1.name; alert(person1,name); You can use the hasOwnProperty() method to check whether a property exists in the instance or in the stereotype. alert(person1.hasOwnProperty("name"))Copy the code

Returns true if it comes from an instance, false if it comes from a prototype

alert("name" in person1); //trueCopy the code

Returns true whether the property is in the stereotype or the instance.

There is also a simpler prototype syntax,

function Person(){} Person.prototype={ name:"nicholas", age:29; sayName:function(){ alert(this.name); }};Copy the code

In the code above, we set Person.prototype to equal a new object created as a literal. The end result is the same, with one exception: the constructor attribute no longer points to Person. Each time a function is created, its Prototype object is also created, and this object automatically gets the constructor property. The syntax we use here, however, essentially overrides the default Prototype Object, so that the constructor property becomes the constructor property of the new Object (pointing to the Object constructor) instead of pointing to the Person function.

var friend=new Person(); alert(friend.constructor==Person); //false alert(friend.constructor==Object); //trueCopy the code

If the constructor value is important you can set it back:

function Person(){} Person.prototype={ constructor:Person, name:"nicholas", age:29; sayName:function(){ alert(this.name); }};Copy the code

Using prototypes, you can also override native objects.

String.prototype.startsWith=function(text){
  //

}

Copy the code

The prototype pattern omits passing initialization parameters to the constructor, resulting in all instances getting the same property value by default.

All properties in a stereotype are shared by many instances, but the problem is more pronounced for properties that contain a value of a reference type.

function Person(){ } Person.prototype = { constructor: Person, name : "Nicholas", age : 29, job : "Software Engineer", friends : ["Shelby", "Court"], sayName : function () { alert(this.name); }}; var person1 = new Person(); var person2 = new Person(); person1.friends.push("Van"); alert(person1.friends); //"Shelby,Court,Van" alert(person2.friends); //"Shelby,Court,Van" alert(person1.friends === person2.friends); //trueCopy the code