First, the factory model
Considering the inability to create a class in ECMAScript, the developer invented a function that encapsulates the details of creating an object with a specific interface.
function Createperson (name,age,job) { var o=New Object (); O.name=name; O.age= Age; O.job=job; O.sayname=function () { alert (this. Name); }; return o;}
var Person1=createperson ("Small Size Farm", 18, "yard Farm");
var Person1=createperson ("Old Code Farmer", 38, "yard Farmer");
Although the factory model solves the problem of creating multiple similar objects, it does not solve the problem of object recognition, that is, how to know the type of an object. (How does this sentence understand???? )
Second, the structural function mode
function Person (name,age,job) { This. name=name; This. age=Age ; This. job=job; This. sayname=function () {alert ( This. Name); };}varperson1=NewPerson ("Small Yards Farm", -,"Yard Farm");varperson1=NewPerson ("Old yards Farm", -,"Yard Farm");
Different from the Createperson () of the engineering model:
1, not explicitly create objects;
2, directly assigns the attribute and the method to this object;
3, no return statement;
An object is created as an instance of an object and an instance of the person
// true // true // true // true
Third, prototype mode
prototype is the prototype object of the object instance that was created when the constructor was called. The advantage of using a prototype object is that all object instances share the properties and methods that he contains.
function Person () {}person.prototype.name="Leo"; Person.prototype.age= in; Person.prototype.job="Software engineer"; Person.prototype.sayName=function () {alert ( This. name);};varperson1=NewPerson ();p erson1.sayname (); //LeovarPerson2=NewPerson ();p erson2.sayname (); //LeoAlert (Person1.sayname==person2.sayname);//true
Unlike the constructor pattern, these properties and methods of the new object are shared by all instances . Both Person1 and Person2 access the same set of properties and the same Sayname () method.
Each instance of person-----person1 and Person2 contain an intrinsic property that points only to person.prototype; in other words, they have no direct relationship to the constructor function.
You can use the isprototypeof () method to determine the existence of this relationship between objects. Such as:
Alert (Person.prototype.isPrototypeOf (Person1)); // truealert (Person.prototype.isPrototypeOf (Person2)); // true
The object.getprototypeof () method returns the value of [[Prototype]]. Such as:
// truealert (object.getprototypeof (person1). name); // Leo
Whenever the code reads a property of an object, the search begins with the cashing instance itself, returns the value of the property if it finds a property of the given name, or, if it is not found, continues to shrink the prototype object pointed to by the pointer, looking for the property with the given name in the prototype object. The value of the property is returned. That is, if we call Person.sayname (), we perform two searches successively. First, the parser asks: "Does the instance Person1 have a sayname attribute?" Answer "No" and then he goes on to ask, "Is there a sayname attribute in Person1 's prototype?" Answer: Yes, read the function that was saved in the prototype object.
If we add a property to the instance with the same name as an attribute in the instance prototype, we create the property in the instance that will mask that property in the prototype.
Use the hasownproperty () method to detect whether a property exists in an instance or exists in a prototype. This method is intended to return true if the given property exists in the object instance.
JavaScript Object-Oriented learning notes (ii)--Create objects (seven modes)