The previous section has written three methods for creating objects, and there are three modes for creating objects through functions, namely Factory mode, constructor mode, and prototype mode . The most common of these three models is the prototype model . or the chestnut:
Factory mode:
function Fun1 (name,age) { var obj = {}; = name; = Age ; function () { returnthis. name; }; return obj;} var = Fun1 (' Xiaohong ', p1); var p2 = Fun1 (' xiaoming ',+// false
The disadvantage of Factory mode is that object recognition is not possible, that is, the type of an object is known. (PS: Actually I do not understand the usefulness, write too little code)
Constructor mode:
function person (name,age) { this . Name = name; this . Age = this . Sayname = function () {console.log ( ' Hello, I'm called: ' + this .name);};} var p1 = new person (' xiaoming ', 25); var p2 = new person (' Floret ', 23);p 1 instanceof person; //
Creating an object using the constructor pattern requires you to create an instance using the new operator, and calling the constructor in this way goes through the following 4 procedures:
1. Create a new object
2. Assign the scope of the constructor to the new object so that this points to the new object
3. Executing the code in the constructor
4. Returning new objects
You can now use instanceof to detect object types.
However, it is also a disadvantage to simply create objects through constructors, and the disadvantage is to create instances repeatedly, such as the Sayname () method in the code above is created two times.
Combine the constructor pattern with the prototype pattern:
Because the method Sayname () is shareable, so we do not need to let it be created many times, in order to solve this problem, we can put the method Sayname () out, put in the function Persorn outside, but this does not show the encapsulation, so this time needed is the prototype mode.
The constructor pattern is used to define the properties of an instance, which is used to define shared methods and properties.
functionPerson (name,age) { This. Name =name; This. Age =Age ;} Person.prototype.sayName=function() {Console.log (' Hello, I call: ' + This. name);};varP1 =NewPerson (' Xiao Ming ', 25);varP2 =NewPerson (' Floret ', 23);p 1.sayName (); //Hello, my name is: Xiao MingP2.sayname ();//Hello, my name is: Floret
Prototype prototype and __PROTO__
Each function has a prototype (prototype ) attribute, which points to an object that contains the properties and methods shared by all instances .
Once a new instance is created from the constructor, the instance will have a pointer [[Prototype]], which we can access through the __proto__ property, which exists between the prototype objects of the instance and the constructor: Person.prototype = = = person.__proto__.
isPrototypeOf (instence)
The isPrototypeOf () method is used to determine the relationship of the instance to the prototype object of the build function .
// true
Object.getprototypeof (instence)
Object.getprototypeof () Returns the prototype object that created the build function for This instance object.
Object.getprototypeof (p1) = = = Person.prototype; // true
hasOwnProperty ()
hasOwnProperty () can be used to detect whether a property exists in an instance object .
For-in
In the for-in loop, returns all the accessible, enumerable properties in the instance and prototype .
functionPerson (name,age) { This. Name =name; This. Age =Age ;} Person.prototype.sayName=function() {Console.log (' Hello, I call: ' + This. Name + '. Gender: ' + This. sex);}; Person.prototype.sex= ' Male ';varP1 =NewPerson (' Xiao Ming ', 25);p 1.sayName (); //Hello, my name is: Xiao Ming. Gender: Male for(varIinchp1) { if(P1.hasownproperty (i)) {console.log (i); //Name and age }}
Object.keys ()
Object.keys () Returns all enumerable instance properties on a given object.
Object.keys (person.prototype); // ["Sayname", "Sex"]
Object.getownpropertynames ()
Object.getownpropertynames () Returns all instance properties on the given object, including non-enumerable instance properties.
Object.getownpropertynames (person.prototype); // ["Constructor", "Sayname", "Sex"]
Rookie quick fly JavaScript object, prototype, inheritance (ii)