Javascript-Object-Oriented inheritance

Source: Internet
Author: User

1. Prototype chain inheritance (the prototype object of the constructor equals the object instantiated by another constructor)

function Box() {

this.name=' Lee ';

}

function Desk() {

this.age= +;

}

Desk.prototype=new box ();//desk inherited box, forming a prototype chain;

var desk=new desk ();

alert (desk.name);//lee

function Table () {

this.height=100;

}

Table.prototype=new Desk ();//Table Inherits Desk

var table=new table ();//table has two inherited properties

Use the prototype chain to inherit the existing problem:

1: There is a problem with data modification when there is a reference type in the prototype

2: Object of subclass type cannot pass arguments to parent class

2. Object Impersonation

function Test(age) {

this.family=["Mom","Dad"];

this.age=age;

}

function Util(age) {

Test.call (this,age); //Indicates which object to call the test function

//this is the Util object we created.

}

Object posing problem: Cannot use prototype all cannot implement data sharing

3. Combination Inheritance (prototype chain and object impersonation)

function Test(age) {

this.family=["Mom","Dad"];

this.age=age;

}

Test.prototype. Fun=function () {

return this.age+this.family;

}

function Util(age) {

Test.call (this,age); //Object Impersonation

}

Util.prototype=new Text (); This is the prototype chain .

var u=new Util (+);

alert (u.age);

4. Prototype inheritance// O is an object

function getobj(o) {//Return prototype is an object of O

function F() {}

F.prototype=o;

return new F ();

}

Prototype Object

var person={

Name:"Tom",

Family:{' Mom ', ' Dad '}

}

var obj1=getobj (person);

5. Parasitic Inheritance (prototype + Factory mode)

function getobj(o) {

function F() {};

f.prototype=o;

Obj.arr=["html",' css ');

return new F ();

}

Parasitic functions

function createobj(o) {

var obj=getobj (o);

return obj;

}

Prototype Object

var person={

name:"Han",

Age:

}

var a=createobj (person);

Javascript-Object-Oriented inheritance

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.