Object-oriented and prototype (III)

Source: Internet
Author: User

Inherited:

Inheritance is a relatively central concept in object-oriented. Other Orthodox object-oriented languages can be implemented in two ways

Commitment: One is an interface implementation, and the other is inheritance. While ECMAScript only supports inheritance, it does not support interface implementations, and real

The way it is inherited relies on the prototype chain to complete.

function Box () {//box construction
This.name= ' Lee ';
}
function Desk () {//desk construct
this.age=100;
}
Desk.prototype=new Box (); Desc inherits the box, through the prototype, forming the chain
var desk=new desk ();
alert (desk.age);
alert (desk.name); Get the Inherited property
function Table () {//table construct
This.level= ' AAAAA ';
}
Table.prototype=new Desk (); Continuation of the prototype chain inheritance
var table=new table ();
alert (table.name); Inherited box and desk.

prototype chain inheritance diagram

In JavaScript, the inherited function is called the supertype (parent, base class, other language), and the inherited function is called

Sub-type (subclass, derived class). Inheritance also has previous problems, such as literal rewriting of prototypes that break relationships, using reference-type prototypes

, and the subtype cannot pass parameters to the super type. In order to solve the problem of reference sharing and super type not being able to pass parameters, we use a

It is called the technique of borrowing a constructor, or the technique of impersonating an object (forgery object, classic inheritance) to solve both of these problems.

function Box (age) {

this.name=[' Lee ', ' Jack ', ' Hello '

This.age=age;

}

function Desk (age) {

Box.call (This,age); Object impersonating, giving the super type a parameter

}

var desk = new Desk (200);

alert (desk.age);

alert (desk.name);

Desk.name.push (' AAA '); New data added, only for desk

alert (desk.name);

Although the borrowing of constructors solves just two kinds of problems, there is no prototype, and reuse is impossible to talk about. So, we need

The prototype chain + borrows the constructor's pattern, which becomes a composite inheritance.

function Box (age) {

THIS.name = [' Lee ', ' Jack ', ' Hello ']

This.age = age;

}

Box.prototype.run = function () {

return this.name + this.age;

};

function Desk (age) {

Box.call (this, age); Object Impersonation

}

Desk.prototype = new Box (); Prototype chain inheritance

var desk = new Desk (100);

Alert (Desk.run ());

There is also an inheritance pattern called: Prototype inheritance, which uses prototypes and creates new objects based on existing objects.

You do not have to create custom types as well.

function obj (o) {//pass a literal function

function F () {}//Create a constructor

F.prototype = O; Assigning a literal function to a prototype of a constructor

return new F (); Finally returns the instantiated constructor

}

var box = {//literal object

Name: ' Lee ',

Arr: [' elder brother ', ' sister ', ' sister ']

};

var box1 = obj (box); Passed

alert (box1.name);

Box1.name = ' Jack ';

alert (box1.name);


alert (Box1.arr);

Box1.arr.push (' parents ');

alert (Box1.arr);

var box2 = obj (box); Passed

alert (box2.name);

alert (Box2.arr); The reference type shares the

Parasitic inheritance combines the prototype + factory model to encapsulate the process of creating objects.

function Create (o) {//Encapsulation creation process

var f= obj (o);

f.run= function () {

return This.arr; Similarly, references are shared

};

return F;

}

Combined inheritance is the most commonly used inheritance mode for JavaScript, but there is also a small problem with combined inheritance, which is the super-

Type is called two times during use: the first time a subtype is created, and the other is at the part of the sub-type constructor.

function Box (name) {

THIS.name = name;

This.arr = [' elder brother ', ' sister ', ' parents '];

}

box.prototype.run= function () {

Returnthis.name;

};

function Desk (name, age) {

Box.call (this, name); Call Box for the second time

This.age = age;

}

Desk.prototype = Newbox (); Call Box for the first time

The above code is the previous combination of inheritance, then the parasitic combination inheritance, resolved the problem of two calls.

function obj (o) {

Functionf () {}

F.prototype= o;

return new F ();

}

function Create (Box,desk) {

var f=obj (Box.prototype);

F.constructor=desk;

Desk.prototype=f;

}

function Box (name) {

This.name=name;

this.arr=[' brother ', ' sister ', ' parents ';

}

Box.prototype.run=function () {

return this.name;

};

function Desk (name,age) {

Box.call (This,name);

This.age=age;

}

Create (Box,desk); Inheritance is achieved through this

var desk=new desk (' Lee ', 100);

Desk.arr.push (' elder sister ');

alert (Desk.arr);

Alert (Desk.run ()); Only methods are shared

var desk2=new Desk (' Jack ', 200);

alert (Desk2.arr); Reference Problem Resolution

Object-oriented and prototype (III)

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.