Factory mode
The factory pattern solves the problem of creating multiple similar objects, but does not solve the object recognition problem.
function Createperson (name, age, Job) { var o = new Object (); O.name = name; O.age = age; O.job = job; O.sayname = function () { console.log (this.name); } return o;} var = Createperson (' Alice ', Person1, ' PHP Engineer ');p erson1.sayname ();
constructor function
Characteristics of the constructor:
- Create objects without displaying them
- Assign properties and methods to the This object directly
- No return statement
function person ( name, age, Job) { this.name = name; This.age = age; This.job = job; This.sayname = function () { console.log (this.name); }} var = new Person (' Alice ', Person1, ' PHP Engineer ');p erson1.sayname ();
The only difference between constructors and other functions is that they are called differently. Any function that is called by new will be used as a constructor.
Problems with constructors
The constructor mode is useful, but the methods defined within the constructor are recreated on each instance. For example, to define a person1,person2, there is a method named Sayname, but the two methods are not the same Function instances. Functions with the same name on different instances are not equal.
Console.log (Person1.sayname = = person2.sayname)
Prototype mode
Each function we create has a prototype property, which is a pointer to an object. The purpose of this object is to include properties and methods that can be shared by all instances of a particular type.
person = new person (); Person.prototype = { constructor: ' Person ', name: ' Haw ', age:31, job: ' Software Engineer ', Sayname:function () { console.log (this.name); }}; var person1 = new Person ();p erson1.name = ' Sam '
Problems with prototype objects
All properties in the prototype are shared by many instances, and are problematic for properties that contain reference type values.
function person () {}person.prototype = { Constructor:person, name: ' Michel ', age:25, job: ' Software Engineer ', friends: [' Shelby ', ' Court '], sayname:function () { console.log (this.name); }}; var person1 = new Person (), var person2 = new Person ();p erson1.friends.push (' Van '); Console.log (person2.friends); ["Shelby", "Court", "Van"]
Person1 changes to the Friends array affect the Person2
Combining the constructor pattern with the prototype pattern
The most common way to create custom types is by combining the constructor pattern with the prototype pattern.
- Constructor mode: defining instance Properties
- Prototype mode: Define properties for methods and instance shares
function person (name, age, Job) { this.name = name; This.age = age; This.job = job; This.friends = [];} Person.prototype = { Constructor:person, sayname:function () { console.log (this.name); }} var = new person (' Micholas ', Person1, ' software Engineer '), var person2 = new Person (' Greg ', ' Doctor '); Person1.friends.push (' Alice ', ' Sam ');p erson2.friends.push (' Bob '); Console.log (Person1.friends, person2.friends); ["Alice", "Sam"] ["Bob"]
Parasitic constructor mode
Create a function that simply encapsulates the creation of the object code and then returns the newly created object.
function person ( name, age, Job) { var o = new Object (); O.name = name; O.age = age; O.job = job; O.sayname = function () { console.log (this.name); } return o;} var friend = new person (' Micholas ', software, ' Engineer '); Friend.sayname ();
In addition to using the new operator and using the wrapper function called the constructor, this function is exactly the same as the factory pattern.
function Specialarray () { var values = new Array (); Values.push.apply (values, arguments); values.topipestring = function () { return this.join ("|"); }; return values;} var colors = new Specialarray (' Red ', ' blue ', ' green '); Console.log (colors.topipestring ()); Red|blue|green
In the example above, we have created an array with a special method, but we cannot modify the array constructor directly, so we have also used the parasitic construction pattern. With respect to the parasitic construction pattern, the returned object is not related to the constructor or to the stereotype property of the constructor. That is, the object returned by the constructor is not different from the object created outside the constructor. For this reason, you cannot rely on the instanceof operator to determine the object type.
JavaScript Advanced Programming: Object-oriented Programming