Javascript Object-oriented programming - create objects


Object constructor literal or object can be used to create the single object, but the use of these methods to create many objects with the same interface, a large amount of duplicate code. To solve this problem, a variant of the factory pattern began to be used.

1, the factory model

factory pattern abstract the process of creating a specific object. Because ECMAScript not create class developers invented a function, a function to encapsulate specific details to create interface objects, such as:

 1 function createPerson(name, age, job){
 2     var o = new Object();
 3     o.name = name;
 4     o.age = age;
 5     o.job = job;
 6     o.sayName = function(){
 7         alert(this.name);
 8     }
 9     return o;
10 }
11 var person1 = createPerson("Anna", 26, "Software Engineer");
12 var person2 = createPerson("Lily", 24, "Teacher");

It can be called many times createPerson () function, which returns an object each time a property that contains three methods.

Factory mode Although you can create multiple objects, but you can not identify the type of object.

2, model constructor

constructor to create specific types of objects, including native constructors (e.g., Object, Array) and custom constructors.

Custom constructors, object types define custom properties and methods, such as:

. 1  function the Person (name, Age, Job) {     // convention constructor uppercase letter, non constructor start with a letter 
2      this .name = name;    // constructor directly assigned to the attributes and methods of this object 
. 3      the this .age = Age;
 . 4      the this .job = Job;
 . 5      the this .sayName = function () {
 . 6          Alert ( the this .name);
 . 7      };
 . 8  }
 . 9  // Create a new operator Person new instance 
10  var = PERSON1 new new the Person ( "Anna", 26 is, "Software Engineer" );
 . 11  var person2 = new Person("Lily", 24, "Teacher");

Using the new operator calls the constructor manager following four steps:

(1) create an object;
(2) the scope of the assignment to the new object constructor (so this points to the new object);
(3) configured to execute function code (added to the new object attributes).
(4) returns a new object.

Examples of objects that are created have a constructor (the constructor) property, which points to the Person, such as:

Alert (== person1.constructor the Person); // to true

object constructor property originally used to identify the object type. However, the detection target type, instanceof more reliable. Examples of objects created in both the Object instance, is an example of Person. Such as:

Alert (PERSON1 the instanceof Object); // to true for all objects inherit from the Object
Alert (PERSON1 the instanceof the Person); // to true

local mode is better than the constructor factory mode: from Example constructor defined in the future may be identified as One particular type.

Constructors drawback: each method must be re-created again in each instance. However, Function create multiple instances to accomplish the same tasks is not necessary.

3, prototype pattern

each have a function to create the prototype (prototype) attribute, a pointer to an object that contains the properties and methods that can be shared by all instances of a particular type.

The benefits of using a prototype of an object: can make all object properties and methods it contains examples of sharing.

 1 function Person(){
 2 }
 3 Person.prototype.name = "Anna";
 4 Person.prototype.age = 29;
 5 Person.prototype.job = "Doctor";
 6 Person.prototype.sayName = function(){
 7     alert(this.name);
 8 };
 9 var person1 = new Person();
10 person1.sayName();  // "Anna"
11 var person2 = new Person();
12 person2.sayName();  // "Anna"
13 alert(person1.sayName == person2.sayName);  //true

4, a combination mode and a prototype model constructor

most widely used, the highest degree of identity to establish a custom type of method, reference types defined default mode.

The constructor is used to define an instance attribute mode, prototype model used to define the methods and shared properties.

Each instance has its own copy of the instance attribute, but at the same time sharing with reference to each other, to maximize the saving memory.

Support passing parameters to the constructor.

 1 function Person(name, age, job){
 2     this.name = name;
 3     this.age = age;
 4     this.job = job;
 5     this.friends = ["Shelby", "Court"];
 6 }
 7 Person.prototype = {
 8     constructor: Person,
 9     sayName: function(){
10         alert(this.name);
11     }
12 }
13 var person1 = new Person("Anna", 26, "Software Engineer");
14 var person2 = new Person("Greg", 27, "Doctor");
15 
16 person1.friends.push("Van");
17 alert(person1.friends);  // "Shelby,Court,Van"
18 alert(person2.friends);  // "Shelby,Court"
19 alert(person1.friends === person2.friends);  //  false
20 alert(person1.sayName === person2.sayName);  //  true

5, the dynamic prototype model

all the information encapsulated in the constructor, by the initialization in the constructor prototype (only when necessary), while maintaining the advantages of using the constructor and prototypes.

. 1  function the Person (name, Age, Job) {
 2      // property 
. 3      the this .name = name;
 . 4      the this .age = Age;
 . 5      the this .job = Job;
 . 6      // method 
. 7      IF ( typeof  the this .sayName = "! function ") {   // can check the property or method may be any one 
. 8          Person.prototype.syaName = function () {
 . 9              Alert ( the this .name);
 10          };
 . 11      }
 12 is  }
 13 is var friend = new Person("Anna", 27, "Software Engineer");
14 friend.sayName();

Here modifications to the prototype made, can be reflected immediately in all instances.

NOTE: Dynamic prototype model, can not be used to rewrite the prototype object literal. If you have created a prototype rewrite instance, it will break the link between the existing and new prototype instance.

6, spurious mode constructor

to create a function object code that creates the package, returns the newly created object.

. 1  function the Person (name, Age, Job) {
 2      var O = new new Object ();
 . 3      o.name = name;
 . 4      o.age = Age;
 . 5      o.job = Job;
 . 6      o.sayName = function () {
 . 7          Alert ( the this .name);
 . 8      };
 . 9      return O;   // constructor does not return a value when the default returns a new object instance; when the return value is returned, the value returned by calling the constructor can override 
10  }
 . 11  var Friend = new new the Person ( "Anna", 27, "Software Engineer" );
 12 is friend.sayName();  // "Anna"

Suppose you create a special array of additional method, because it is not directly modify the Array constructor, so you can use this pattern:

. 1  function SpecialArray () {
 2      // create an array 
. 3      var values = new new the Array ();
 . 4      // add value 
. 5      values.push.apply (values, arguments);
 . 6      // addition method 
. 7      values.toPipedString = function () {
 . 8          return  the this .join ( "|" );
 . 9      };
 10      // returned array 
. 11      return values;
 12 is  }
 13 is  var Colors = new new SpecialArray ( "Red", "Blue", "Green" );
14 alert(colors.toPipedString());  // "red|blue|green"

There is no relationship between the object and the prototype property of the constructor returns, the Object constructor returns with an object outside the constructor creates no different.

Instanceof operator can not rely on determined object types. Recommended in the case of using other modes, do not use this mode.

7, the constructor secure modes

and secure objects: no public property, which this method is not a reference to the object. Sound objects best suited in some of the security environment (which prohibit the use of this environment and new), to prevent or used when data has been changed by another application.

. 1  function the Person (name, Age, Job) {
 2      // create objects to be returned 
. 3      var O = new new Object ();
 . 4      // private variables and attributes can be defined here 
. 5  
. 6      // addition method 
. 7      o.sayName = function () {
 . 8          Alert (name);
 . 9      };
 10      // returns the object 
. 11      return O;
 12 is  }
 13 is  var Friend the Person = ( "Anna", 27, "Software Engineer" );
 14 friend.sayName (); / / "Anna" In addition to calling sayName () method, there is no other way to access their data members
15  //   very suitable for certain secure execution environment

Note:
there is no relationship between safe mode constructor to create the object and the constructor, so instanceof operator for such objects have no meaning.





Guess you like

Origin www.cnblogs.com/wuxxblog/p/11210577.html