JavaScript Inherits notes----1024

Source: Internet
Author: User

Waves Essay JavaScript inherited notes

Prototype (prototype): A prototype is an object that other objects can use to implement property inheritance

/* Notes:
* 1. Class-based inheritance: the way to inherit from the prototype chain

* 2. Prototype inheritance: Encapsulation of class-inheritance

* 3. Parasitic inheritance: Two packages inherited from the prototype, and extended for inherited objects during the second encapsulation

* 4. Constructor-style inheritance: How to inherit by constructor

* 5. Combined inheritance (class-Type inheritance + constructor-style inheritance Both advantages add filter disadvantage)

6. Parasitic combined inheritance: the advantages of parasitic inheritance fusion constructor-style inheritance remove the disadvantage of the way
*/


Prototype inheritance
function Inheritobject (o) {
Declaring a transition function object
function F () {
}
Transition prototype object Inherits parent object
F.prototype=o;
Returns a real column of a transition object that inherits the parent object's prototype
return new F ();
}

var book={
Name: "Jsbook",
alikebook:[' CSS book ', the ' HTML book ',
};

Parasitic inheritance (a second encapsulation of the prototype inheritance, and an extension of the inherited object during the second encapsulation process)
Declaring base objects
function Createbook (obj) {
Creating a new object from a prototype inheritance method
var o = new Inheritobject (obj);
Extending new objects
O.getname=function () {
Console.log (this.name);
}
Returning an extended object
return o;
}

/*
* Parasitic inheritance-inheriting prototypes
* Pass-through parameter subclass subclass
* Pass parameter Superclass parent class
* */
function Inheritprototype (subclass,superclass) {
Copy a copy of the prototype of a parent class saved in a variable
var p=inheritobject (Superclass.prototype);
Fix the constructor property of a subclass is modified because the subclass prototype is overridden
P.constructor=subclass;
Set Subclass prototypes
Subclass.prototype=p;
}

Defining the Parent class
Function superclass (name) {
THIS.name = name;
this.colors=["Red", "Blue", "green"];
}

Defining a parent class prototype method
Superclass.prototype.getname=function () {
Console.log (this.name);
}

Defining subclasses
function Subclass (Name,time) {
constructor-style inheritance
Superclass.call (This,name);
Sub-class new properties
This.time=time;
}

Parasitic inheritance of parent class prototypes
Inheritprototype (Subclass,superclass);

New prototype method for sub-class
Subclass.prototype.getTime = function () {
Console.log (This.time);
}


Single-Inheritance Extend property replication
var extend=function (Target,source) {
Traversing properties in the source object
For (Var property in source) {
Copy the properties from the source object into the target object
Target[property]=source[property];
}
return target;
}

Multiple-inheritance property replication can be bound to a native object on an object
Object.prototype.mix=function () {
var i=0,//from the first argument as an inherited object
Len=arguments.length,//arguments is equivalent to a collection of multiple pass parameters, very similar to an array
target=arguments[0];//The first incoming parameter is the target object
Arg Cache Parameter Object
Traversing an inherited object
for (; i<len;i++) {
Cache current Object
Arg=arguments[i];
For (Var property in Arg) {
Copy the attributes from the inherited object to the target object
This[property]=arg[property];
}
}

}


Test Order Inheritance
var b1={
Name: ' JavaScript design mode ',
alike:[' CSS ', ' html ', ' JavaScript ']
}

var b2={
Color: ' Blue '
}
Extend (B2,B1);
Console.log ("Single Inheritance Test:" +b2.name+ "-" +b2.alike+ "-" +b2.color ");




/* Parasitic combined inheritance test begin*/
Console.log ("Parasitic combined inheritance test");
var instance1=new subclass ("JS book", 2014);
var instance2=new subclass ("CSS book", 2013);

Instance1.colors.push ("Black");
Console.log (instance1.colors);
Console.log (instance2.colors);

Instance1.getname ();
Instance1.gettime ();

Instance2.getname ();
Instance2.gettime ();


/* Parasitic combined inheritance test end*/

/* Parasitic inheritance test begin*/
Console.log ("Parasitic inheritance test");
var newbook=createbook (book);
Newbook.name= "Ajax book";
NewBook.alikeBook.push ("XML book");

var otherbook=createbook (book);
Otherbook.name= "Flash book";
OtherBook.alikeBook.push ("as book");

Console.log (Newbook.name);
Console.log (Newbook.alikebook);

Newbook.getname ();//The object was encapsulated two times and extended

Console.log (Otherbook.name);
Console.log (Otherbook.alikebook);
Otherbook.getname ();
/* Parasitic inheritance test end*/

/*json Object Test */
Console.log (book.name+ ":" +book.alikebook);

Test multiple inheritance
Otherbook.mix (B1,B2);
Console.log (Otherbook);

JavaScript Inherits notes----1024

Related Article

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.