Js-oriented prototype language summary, js-oriented prototype Language
1. Search and delete Arrays
Function WarrperArray (array ){
This. array = array;
}
// Returns the subscript.
WarrperArray. prototype. existValue = function (val ){
For (var j = 0; j <this. array. length; j ++ ){
If (this. array [j] = val) return j;
}
Return-1;
}
// Delete the specified Element
WarrperArray. prototype. removeValue = function (val ){
Var index = this. array. indexOf (val );
If (index>-1 ){
This. array. splice (index, 1 );
Return this. array;
}
}
Ii. Summary of the prototype Constructor
/**
// Js Prototype object constructor, wasting memory, defining unchanged attributes and functions in Prototype mode
Function Cat (name, color ){
This. name = name;
This. color = color;
}
// Define common methods and attributes on the prototype to save memory overhead.
Cat. prototype. type = "TYPE_CAT"
Cat. prototype. eat = function (){
Alert ("eat mouse ");
}
*/
Iii. Inheritance of constructor function Animal (){
This. species = "animal ";
}
/**
// Constructor
Function Cat (name, color ){
This. name = name;
This. color = color;
}*/
// Inheritance Method 1 constructor binding (sub-classes have attributes and methods of the parent class) constructor is a sub-class
Function Cat (name, color ){
Animal. apply (this, arguments );
This. name = name;
This. color = color;
}
// Inheritance Method 2 prototype mode
/**
Cat. prototype = new Animal (); // assign the parent class to the subclass prototype
Cat. prototype. constructor = Cat; // sets the constructor as a subclass.
*/
/** Use an empty object as an intermediary
Var F = function (){};
F. prototype = Animal. prototype; // assign the prototype of the parent class to an empty object.
Cat. prototype = new F (); assign the mediation instance to the sub-Object
Cat. prototype. constructor = Cat;
*/
4. encapsulate the function extend (Child, Parent) into a function extend (Child, Parent ){
Var F = function (){};
F. prototype = Parent. prototype;
Child. prototype = new F ();
Child. prototype. constructor = Child;
Child. uber = Parent. prototype;
}
/** Use the following
Extend (Cat, Animal );
Var cat1 = new Cat ("Da Mao", "yellow ");
Alert (cat1.species); // animal
*/
/** 5. Copy and inherit from the constructor */function extend2 (Child, parent ){
Var p = parent. prototype;
Var c = Child. prototype;
For (var I in p ){
C [I] = p [I];
}
C. uber = p; // set the attributes of the parent class.
}
/** 6. Non-constructor inheritance */var Chinese = {nation: 'China '};
Var Doctor = {carteer: 'Doc '};
// The Chinese doctor assigned the prototype of the Child object to the parent object so that the child object can be connected together.
Function object (o ){
Function F (){};
F. prototype = o;
Return new F ();
}
/**
Use the following to generate sub-Objects Based on the parent object
Var Doctor = object (Chinese );
Add the attributes of the sub-object.
Doctor. career = 'Doc ';
At this time, the sub-object inherits the attributes of the parent object.
Alert (Doctor. nation );
*/
/** 7. copy shortest copy */function extendCopy (p ){
Var c = {};
For (var I in p ){
C [I] = p [I];
}
C. uber = p;
}
/** Use the following code */
Var Doctor = extendCopy (Chinese );
Doctor. career = 'Doc ';
Alert (Doctor. nation );
// This copy is shortest copy. If the attribute has arrays and references, the attributes of the subclass are modified, and the attributes of the parent class are also changed;
// Example
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];
// Use the extendCopy () function Doctor to Inherit Chinese
var Doctor = extendCopy(Chinese);
// Add a city for the location of the Doctor:
Doctor. birthPlaces. push ("Henan ");
// View the Chinese characters of the parent object and find that the place of birth is modified.
Alert (Chinese. birthPlaces); // 'beijing', 'shanghai', 'Hong Kong ', Henan
Alert (Doctor. birthPlaces); // same as above
// Therefore, this kind of shallow copy is not applicable to and inherited
// Deep copy
Function deepCopy (p, c ){
Var c = c || {};
For (var I in p ){
If (typeof (I) = 'object '){
C [I] = (p [I]. constructor = Array )? []: {};
DeepCopy (p [I], c [I]); // recursive writing
} Else {
C [I] = p [I];
}
}
}
// Use the following code:
Var Doctor = deepCopy (Chinese );
// Add an attribute to the parent object, location Array
Chinese. birthPlaces = ['beijing', 'shanghai', 'Hong Kong '];
// Modify attributes on the sub-Object
Doctor. birthPlaces. push ("Henan ");
// View the attribute values of these two objects
Alert (Doctor. birthPlaces); // 'beijing', 'shanghai', 'Hong Kong ', Henan
Alert (Chinese. birthPlaces); // 'beijing', 'shanghai', 'Hong Kong'
// Currently, the jQuery library uses this inheritance method.
// The referenced data type can also be returned using the constructor.
// Example:
Function clone (obj ){
Function F (){};
F. prototype = obj;
Return new F ();
}
Var Chinese = {
Nation: 'China ',
CreateBirthPlaces: function (){
Return ['beijing', 'shanghai', 'Hong Kong '];
}
}
Var Doctor = clone (Chinese );
Doctor. career = 'Doc ';
Doctor. birthPlaces = Chinese. createBirthPlaces ();
Alert (Doctor. birthPlaces. length); // 3
Doctor. birthPlaces. push ("Dalian ");
Alert (Doctor. birthPlaces. length); // 4
Alert (Chinese. birthPlaces. length); // 3