JavaScript的继承方式

真正的面向对象语言必须支持继承机制,即一个类能够重用(继承)另一个类的方法和属性。

      所有开发者定义的类都可作为基类,出于安全原因,本地类和宿主类不能作为基类。有时你可能想创建一个不能直接使用的基类,它只是用于给子类提供通用的函数,这种基数被看作抽象类。创建的子类将继承超类的所有属性和方法,包括构造函数及方法的实现。所有的属性和方法都是公用的,子类可直接访问这些方法,还可添加超类中没有的属性和方法,也可覆盖超类中的属性和方法。

一、 继承的方式

      (1) 对象冒充:构造函数使用this关键字给所有属性和方法赋值(即采用声明的构造函数方式)。因为构造函数只是一个函数,所在可使ClassA的构造函数成为ClassB的方法,然后调用它,ClassB就会收到ClassA的构造函数中定义的属性和方法。所有的新属性和新方法都必须在删除了新方法的代码行后定义。

function ClassA(sColor){
        this.color=sColor;
        this.sayColor=function(){
                alert(this.color);
        }
}
function ClassB(sColor){
       this.newMethod=ClassA;
       this.newMethod(sColor);
       delete this.newMethod //删除了对ClassAr的引用
       this.name=sName;
       this.sayName=function(){
                alert(this.name);
       }
}
var objA=new ClassA(“red”);
var objB=new ClassB(“blue”,”Nicholas”);
objA.sayColor(); //outputs “red”
objB.sayColor(); //outputs “blue”
objB.sayName(); //outputs “Nicholas”

      对象冒充支持多重继承,一个类可以继承多个超类。

       (2)、call()方法

       它的第一个参数用作this的对象,其他参数都直接传递给函数自身。

function sayColor(sPrefix,sSuffix){
          alert(sPrefix+this.color+sSuffix);
};
var obj=new Object();
obj.color=”red”;
syaColor.call(obj,”This color is”,”, a very nice color indeed.”);
//outpus “This color is red , a very nice color indeed.”

         要与继承机制的对象冒充方法一起使用该方法,只需将前三行的赋值、调用和删除代码替换即可:

function ClassB(sColor,sName){
         //this.newMethod=ClassA;
         //this.newMethod(sColor);
         ClassA.call(this,sColor);
         this.name=sName;
         this.sayName=function(){
                 alert(this.name);
         };
  }

          (3)、apply()方法

          apply()方法有两个参数,用作this的对象和要传递给函数的参数的数组。

function sayColor(sPrefix,sSuffix){
        alert(sPrefix+this.color+sSuffix);
};
var obj=new Object();
obj.color=”red”;
sayColor.apply(obj,new Array(“The color is ”,”, a very nice color indeed.”));
//outputs “The color is red, a very nice color indeed.”

          该方法也用于替换前三行的赋值、调用和删除新方法的代码。

function ClassB(sColor,sName){
         //this.newMethod=ClassA;
         //this.newMethod(sColor);
         classA.apply(this,new Array(sColor));
          this.name=sName;
          this.sayName=function(){
                   alert(this.name);
         };
}

      (4)、原型链

      prototype对象是个模版,要实例化的对象都以这个模板为基础。prototype对象的任何属性的方法都被传递给那个类的所有实例。

function ClassA(){
}
ClassA.prototype.color=”red”;
ClassA.prototype.sayColor=function(){
         alert(this.color);
};
function ClassB(){
}
ClassB.prototype=new ClassA();
ClassB.prototype.name=””;
ClassB.prototype.sayName=function(){
          alert(this.name);
}

      调用ClassA的构造函数时,没有给它传递参数,这在原型链中是标准做法,要确保构造函数没有任何函数。子类的所有属性和方法都必须出现在prototype属性被赋值后,因为在它之前赋值的所有方法都会被删除。Prototype属性被替换成了新对象,添加了新方法的原始对象将销毁。原型链的弊端是不支持多重继承。

      (5)、混合方式

       用对象冒充继承构造函数属性,用原型链继承prototype对象的方法。

function ClassA(sColor){
          this.color=sColor;
}
ClassA.prototype.sayColor=function(){
          alert(this.color);
};
function ClassB(sColor,sName){
          ClassA.call(this,sColor);
          this.name=sName;
}
ClassB.prototype=new ClassA();
ClassB.prototype.sayName=function(){
          alert(this.name);
};

posted @ 2012-09-14 23:03  风去无痕  阅读(136)  评论(0编辑  收藏  举报