JavaScript package (package)

First, the package of the constructor mode
provides a constructor mode for solving problems from prototype object generating instances, and JavaScript provides a constructor mode.
The so-called “constructor” is actually a normal function, but the THIS variable is used inside. Use the New operator for the constructor to generate an instance, and the THIS variable is bound to the instance object.
For example, the following is the prototype object of the dog:

  Function Dog (Name, Color, Species) { = Name; this.color = Color; this.Species = species; this.type = "Dogs";}  
The example object is as follows:

VAR DOG1 = New Dog (“Rhubarb”, “Yellow”, “China Nationalical Dog”); Var Dog2 = New Dog (“Small Black”, “Black”, “German Black Back”); Alert; // Malon Alert (Dog2.color); // Black This time DOG1 and DOG2 will automatically contain a constructor property and point to their constructor. alert (dog1.constructor == Dog); //truealert(dog2.constructor == Dog); // trueJavascript also provides a instanceof operator to verify the relationship between the prototype objects and the object instance. Alert (Dog1 InstanceOf Dog); // TrueAlert (Dog2 InstanceOf Dog); // True
   However, the constructor pattern also has a waste of memory. Because of each instance object, the Type property is the same, each generation of an instance object, must allocate memory for repeated content, which neither saves, and lacks efficiency. Therefore, we should think of a method that allows Type to generate only once in memory, and all instances point to this memory address. 
Second, constructor binding to the PROTOTYPE mode package

Waste memory issues, there is a solution, The unchanged properties and methods are directly defined on the Prototype object in the constructor.

Function Dog (Name, Color, Species) {this.Name = Name; this.color = color; this.Species = species;} Dog.Prototype.Type = “Dogs”; The generated example object is as follows:

 var DOG1 = New DOG ("Rhubarb" , "Yellow", "China Nationalical Dog"); Var Dog2 = New Dog ("Small Black", "Black", "German Black Back"; Alert (Dog1.Type); // Dog   
At this time, all instances of the Type property are actually the same memory address, pointing to the Prototype object, thus improving the operational efficiency.

Third, Prototype mode verification method

In order to match the prototype property, JavaScript defines some auxiliary methods to help us use it.
 3.1 isprototypeOf ()   This method is used to determine the relationship between a ProPtotype object and an instance. 
Alert (Dog.Prototype.isprototypeof (DOG1)); //true

lert(dog.prototype.isprototypeof(dog2)); // True
3.2 HasownProperty ()

Each instance object has a HASOWNPROPERTY () method to determine whether an attribute is local attribute or inherits the properties from prototype objects. .

Alert (“Name”)); // true

Alert (Dog1.hasownProperty (“Type”)); // False

The in operator can be used to determine whether an instance contains an attribute, whether it is not a local attribute.

Alert (“Name” in dog1); // true

ALERT (“Type” in Dog1); // true

IN operator can also be used to traverse all attributes of an object.

for (VAR PROP IN DOG1) {Alert (“DOG1 [” + Prop + “] =” + DOG1 [PROP]);}


The above is all of this article, I hope this paper has certain reference to study value for everyone’s learning or work.Thank you for your support of Tumi Cloud. If you want to know more about the content, please check the link below

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment