1. Factory mode
Encapsulated with functions.
function Creatperson (name,age,job) {
var 0 = new Object ();
O.name = name;
O.age = age;
O.job = job;
O.sayname = function () {
alert (this.name);
};
return o;
}
var person1 = Creatperson ("TFS", 5, "singer");
var person2 = Creatperson ("TFR", 6, "Painter");
Advantage: The person object was created and can be called countless times.
Disadvantage: Cannot know the type of object.
2, the constructor function pattern.
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 ("TFS", 5, "singer");
var person2 =new person ("TFR", 6, "Painter");
Detection Object type: instanceof
Disadvantage: Each member cannot be reused.
3, prototype mode.
Prototype
[[prototype]]
Constructor: Each time a function is created, its prototype object is created, and the object automatically obtains the constructor attribute.
hasOwnProperty (): the instance defines this property to return true.
such as: Var person1 =new person ();
Person1.name = "TFS";
Alert (Person1.hasownproperty ("name"));//true
In: Object can access the given property and return true, regardless of whether the property is in the instance or in the prototype. Alert ("name" in Person1); True
For-in loop: Returns enumerable properties that are accessible to all objects.
Hasprototypeproperty (): property returns True in the prototype. Alert (Hasprototypeproperty (person, "name"));
Object.keys (): Gets all enumerable instance properties. var keys = Object.keys (Person.prototype); alert (keys); "Name"
Object.getownpropertynames (): All instance properties can be obtained, whether or not enumerated.
The dynamics of a prototype:
function person () {}
var friend = new person ();
Person.prototype = {
Constructor:person; Creates a new object in literal form and no longer points to person if constructor is not specified.
Name: "TFS";
Age:5;
}
alert (friend.name); Error
Prototype Object disadvantage: All instances share an array.
function person () {}
Person.prototype = {
Constructor:person;
Name: "TFS";
Age:5;
friends:["1", "2"];
};
var person1 = new Person ();
var person2 = new Person ();
Person1.friends.push ("3");
alert (person1.friends); "1,2,3"
alert (person2.friends); "1,2,3". All instances are found to share an array.
4. Construction function + prototype mode
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
This.friends =["1", "2"];
};
Person.prototype ={
Constructor:person;
Sayname:function () {
alert (this.name);
}
}
var person1 = new Person ();
var person2 = new Person ();
Person1.friends.push ("3");
alert (person1.friends); "1,2,3"
alert (person2.friends); "1,2"
alert (person1.sayname===person2.sayname); True
5. Dynamic prototype mode
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job; if (typeof this.sayname!= "function") {//Only add it to the prototype if the Sayname () method does not exist.
Person.propotype.sayName = function () {
alert (this.name);
}
}
var person =new person ("TFS", 5, "singer");
Person.sayname ();
6. Parasitic structural function pattern
7, the Safe construction function pattern