Combine the constructor pattern with the prototype pattern:
function person (name, age, Job) { this.name = name; This.age = age; This.job = job; This.friends = [' Shelby ', ' Court ']; } Person.prototype = { Connstructor:person, sayname:function () { return this.name; } } var Person1 = new Person (' Tom ', ' software engineer '); var person2 = new Person (' Jack ', ' Doctor '); Person1.friend.push (' Var '); Console.log (person1.friend); Output ' Shelby ', ' Court ', ' Var ' Console.log (person2.friend); output ' Shelby ', ' Court ', console.log (person1.friend = = = Person2.friend);//Falseconsole.log (person1.sayname = = = Person2.sayname); True
Because the Sayname method belongs to the prototype property, Person1.sayname is identical to person2.sayname and references the same address.
Dynamic Prototyping Mode
function person (name, age) { this.name = name; This.age = age; if (typeof this.sayname = = = ' Fucntion ') { Person.prototype.sayName = function () { alert (this.name); } } //When creating the person instance object for the first time, because the Syaname method does not currently exist, the syntax block in if is executed, the Sayname method is placed on the shared object prototype, and when the person object is later instantiated, Because the Sayname property already exists, it is not created.
Parasitic constructors: (parasitic constructors are similar to factory patterns, except for a new one)
function person (name, age) { var o = new Object (); O.name = name; O.age = age; O.sayname = function () { alert (this.name); } return o;}
Secure constructor Mode
Secure Object : The so-called prudent object, refers to the absence of public properties, and other methods do not reference the This object. A secure object is best for use in some secure environment, or to prevent data from being altered by other applications.
JavaScript object-Oriented programming record Note 4