JS Object-oriented program settings--Inheritance mechanism

Source: Internet
Author: User

Inherited
1. Object posing

/* Function ClassA (scolor) {
This.color = Scolor;
This.saycolor = function () {
alert (This.color);
};
}

function ClassB (Scolor, SName) {
This.newmethod = ClassA;
This.newmethod (Scolor);
Delete This.newmethod;

THIS.name = SName;
This.sayname = function () {
alert (this.name);
};
}

var obja = new ClassA ("Blue");
var objb = new ClassB ("Red", "John");
Obja.saycolor (); Output "Blue"
Objb.saycolor (); Output "Red"
Objb.sayname (); Output "John" */

2, call method, A.call (B,...); A, which represents the parent class, B, which represents the child class. The second parameter indicates the argument passed to the parent class

2.1. Add a method to an object

/* function sss (AA) {
alert (AA);
}

var obj=new Object ();
Obj.obj111= "23545";
Sss.call (obj, "yellow");
*/

2.2. Object Inheritance Object
/* Function ClassA (scolor) {
This.color = Scolor;
This.saycolor = function () {
alert (This.color);
};
}

function ClassB (Scolor, SName) {
Classa.call (this, scolor);
THIS.name = SName;
This.sayname = function () {
alert (this.name);
};
}

var clsb=new ClassB ("Blue", "ADFADF");
Clsb.saycolor (); */

3, apply method, A.apply (B,...); A, which represents the parent class, B, which represents the child class. The second parameter represents the arguments passed to the parent class.
parameter is an array type

3.1. How to add Objects
/* Function aaa (AAA) {
Alert ("AAAA" +aaa);
}

var obj=new Object ();
Obj.name= "2343";

Aaa.apply (obj,new Array ("---bbb")); */

3.2. Object Inheritance Object
/* function ClassA (name) {
This.name=name;
This.showname=function () {
alert (this.name);
};
}

function ClassB (name) {
This.nameb=name;
This.aaa=classa;
This.aaa.apply (this,new Array (THIS.NAMEB));

This.showname1=function () {
Alert (this.nameb+ "... bbbbb");
};
}

var bbb=new ClassB ("CCC");
Bbb.showname ();
Bbb.showname1 (); */

JS Object-oriented program settings--Inheritance mechanism

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.