javascript对象的继承

1. [代码][JavaScript]代码        

01//1、对象冒充
02//说明:构造函数使用this关键字给所有属性和方法赋值(即采用类声明的构造函数方式)。因为构造函数只是一个函数,所以可使ClassA的构造函数成为ClassB的方法,然后调用它。ClassB就会收到ClassA的构造函数中定义的属性和方法。
03function ClassA(sColor) {
04    this.color = sColor;
05    this.showColor = function() {
06        alert(this.color);
07    };
08}
09 
10function ClassB(sColor, sName) {
11    this.newMethod = ClassA;
12    this.newMethod(sColor);
13    delete this.newMethod;
14 
15    this.name = sName;
16    this.showName = function() {
17        alert(this.name);
18    };
19}
20 
21var oa = new ClassA("RED");
22var ob = new ClassB("blue", "red");
23oa.showColor();
24// RED
25ob.showColor();
26// blue
27ob.showName();
28// red

2. [代码][JavaScript]代码        

01//2、call()方法
02//说明:这是与经典的对象冒充方法最相似的方法。它的第一个参数用作this的对象。其他参数都都直接传递给函数自身。其实就相当于前一个是对象,后一个是普通的参数一样。
03function showColor(sPrefix, sSuffix) {
04    alert(sPrefix + this.color + sSuffix);
05};
06var obj = new Object();
07obj.color = "red";
08// the color is red ,a very nice color indeed
09showColor.call(obj, "the color is ", " ,a very nice color indeed");
10function ClassA(sColor) {
11    this.color = sColor;
12    this.showColor = function() {
13        alert(this.color);
14    };
15}
16 
17function ClassB(sColor, sName) {
18    ClassA.call(this, sColor);
19    this.name = sName;
20    this.showName = function() {
21        alert(this.name);
22    };
23}
24 
25var ob = new ClassB("red", "ooo");
26ob.showColor();
27// red
28ob.showName();
29// ooo

3. [代码][JavaScript]代码       

01//3、apply()方法
02//说明:有两个参数,用作this的对象和要传递给函数的参数的数组。
03function showColor(sPrefix, sSuffix) {
04    alert(sPrefix + this.color + sSuffix);
05};
06var obj = new Object();
07obj.color = "green";
08// the color is green ,a very nice color indeed
09showColor.apply(obj, new Array("the color is ", " ,a very nice color indeed"));
10function ClassA(sColor) {
11    this.color = sColor;
12    this.showColor = function() {
13        alert(this.color);
14    };
15}
16 
17function ClassB(sColor, sName) {
18    ClassA.apply(this, arguments);
19    this.name = sName;
20    this.showName = function() {
21        alert(this.name);
22    };
23}
24 
25var ob = new ClassB("red", "ooo");
26ob.showColor();
27// red
28ob.showName();
29// ooo

4. [代码][JavaScript]代码        

01//4、原型链
02// 说明:原型链扩展了类的原型定义方式。prototype对象是个模板,要实例化的对象都以这个模板为基础。prototype对象的任何属性和方法都被传递给那个类的所有实例。原型链利用这种功能来实现继承机制。原型链的弊端是不支持多重继承。记住,原型链会用另一种类型的对象重写类的prototype属性。因此子类的所有属性和方法必须出现在prototype属性被赋值以后。
03// 原型链方式对应原型方式
04function ClassA() {
05}
06 
07ClassA.prototype.color = "red";
08ClassA.prototype.showColor = function() {
09    alert(this.color);
10};
11function ClassB() {
12}
13 
14ClassB.prototype = new ClassA();
15ClassB.prototype.name = "redred";
16ClassB.prototype.showName = function() {
17    alert(this.name);
18};
19var objA = new ClassA();
20var objB = new ClassB();
21objA.color = "blue";
22objA.showColor();
23// blue
24objB.color = "green";
25objB.name = "dodo";
26objB.showColor();
27// green
28objB.showName();
29// dodo
30alert( objB instanceof ClassA);
31// true
32alert( objB instanceof ClassB);
33// true

5. [代码][JavaScript]代码        

01//5、混合方式
02//说明:与定义类时同样的方式,即利用构造函数方式定义属性,用原型方式定义方法。而在继承机制中,可以用对象冒充继承构造函数的属性,用原型链继承prototype对象的方法。
03function ClassA(sColor) {
04    this.color = sColor;
05}
06 
07ClassA.prototype.showColor = function() {
08    alert(this.color);
09};
10function ClassB(sColor, sName) {
11    ClassA.call(this, sColor);
12    this.name = sName;
13}
14 
15ClassB.prototype = new ClassA();
16ClassB.prototype.showName = function() {
17    alert(this.name);
18};
19var objA = new ClassA("BLUE");
20objA.showColor();
21// BLUE
22var objB = new ClassB("red", "baba");
23objB.showColor();
24// red
25objB.showName();
26// baba
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值