JS for prototype Language summary

Source: Internet
Author: User
Tags jquery library

I. Finding and deleting arrays

function Warrperarray (array) {

This.array = array;
}
return 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 a specified element of merit

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 function
/**
JS prototype object constructor, wasting memory, defining invariant properties and functions on prototype prototype mode
function Cat (name,color) {
This.name=name;
This.color = color;
}
Save memory overhead by defining common methods and properties on prototypes
Cat.prototype.type= "Type_cat"
Cat.prototype.eat = function () {
Alert ("Eat Mouse");
}
*/
Third, the Inheritance function Animal () of the constructor {
This.species = "Animal";
}
/**
constructor function
function Cat (name,color) {
THIS.name = name;
This.color = color;
}*/

The inheritance method is a constructor binding (the subclass has properties and methods of the parent class) that the constructor is a subclass of
function Cat (name,color) {
Animal.apply (this,arguments);
THIS.name =name;
This.color = color;
}
Inheritance Method two prototype mode
/**
Cat.prototype = new Animal ();//Assigning a parent class to a subclass prototype
Cat.prototype.constructor = cat;//To set the construction method to child class
*/

/** using empty objects as intermediaries
var F = function () {};
F.prototype = animal.prototype;//assigns the prototype of the parent class to an empty object
Cat.prototype = new F (); Assigning a mediation instance to a child object
Cat.prototype.constructor = Cat;
*/
Four, the use of empty object inheritance encapsulated into a method 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); Animals
*/
/**, constructor copy inherits */function Extend2 (child,parent) {
var p = parent.prototype;
var c = Child.prototype;
for (var i in P) {
C[i] = P[i];
}
C.uber = p;//Leave the properties of the parent class
}

/** Six, the inheritance of non-constructors */var Chinese ={nation: ' China '};
var Doctor = {carteer: ' Doctor '};
Chinese doctor the prototype of the object is assigned to the parent object so that it joins together
function Object (o) {
function F () {};
F.prototype = O;
return new F ();
}

/**
Use the following to generate child objects on the parent object basis

var Doctor = object (Chinese);
Then add the properties of the child object itself
Doctor.career = ' Doctor ';
The child object has inherited the parent object property
alert (doctor.nation);
*/

/** Seven, shallow copy copy*/function extendcopy (p) {
var c = {};
for (var i in P) {
C[i] = P[i];
}
C.uber = p;
}

/** Use the following * *
var Doctor = extendcopy (Chinese);
Doctor.career = ' Doctor ';
alert (doctor.nation);

This copy is shallow copy if the attribute has arrays and references, the properties of the subclass are modified, and the properties of the parent class change;
Example
chinese.birthplaces=[' Beijing ', ' Shanghai ', ' Hong Kong ';
Inherit Chinese through the extendcopy () function doctor
var Doctor = extendcopy (Chinese);
Then add a city to the birthplace of doctor:
Doctor.birthPlaces.push ("Henan");
View the Parent object Chinese, find the place of birth was modified
alert (chinese.birthplaces);//' Beijing ', ' Shanghai ', ' Hong Kong ', Henan
alert (doctor.birthplaces);//Ibid.
So this shallow copy does not apply with inheritance


//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 notation
}else{
C[i] = P[i];
}
}
}

Use the following:
var Doctor = deepcopy (Chinese);
Adds a property to the parent object, the birthplace array
chinese.birthplaces=[' Beijing ', ' Shanghai ', ' Hong Kong ';
Then modify the properties on the child object
Doctor.birthPlaces.push ("Henan");
View the property values for both objects
alert (doctor.birthplaces);//' Beijing ', ' Shanghai ', ' Hong Kong ', Henan
alert (chinese.birthplaces);//' Beijing ', ' Shanghai ', ' Hong Kong '

Currently, the jquery library is using this method of inheritance.


The reference data type can also be returned with the constructor function

Examples are as follows:
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 = ' Doctor ';
Doctor.birthplaces=chinese.createbirthplaces ();
alert (Doctor.birthPlaces.length);//3
Doctor.birthPlaces.push ("Dalian");
alert (Doctor.birthPlaces.length);//4
alert (Chinese.birthPlaces.length);//3

JS for prototype Language summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.