counter
counter

javascript继承

1.call()继承方式:与对象冒充方式最相似

 一. function saycolor(pre,nex) {
            alert(pre+this.color+nex);
        }
 
        var obj = new Object;
        obj.color = "RGB(0,100,0)";
        saycolor.call(obj,"pre表示前置语句","nex表示后置语句")
   call参数,obj代表需要调用的对象,后两个参数为自身的参数。


二.ClassB 继承ClassA            function ClassA(color) {                 this.color = color;                 this.SayColor = function () {                     alert(this.color);                 }             } //call参数,第一个this代表函数对象ClassB本身,第二个colors代表该函数对象的参数。             function ClassB(colors, name) {                 ClassA.call(this, colors);                 this.name = name;                 this.SayName = function () {                     alert(this.name);                 };             }             function ShowClass() {                 var colors = new ClassB("RGB(0,100,0)""绿色");                 colors.SayColor();                 colors.SayName();             }

2.apply()继承方式
一.
     function saycolor(pre,nex) {
            alert(pre+this.color+nex);
        }
 
        var obj = new Object;
        obj.color = "RGB(0,100,0)";
        saycolor.apply(obj, new Array("pre表示前置语句""nex表示后置语句"));


二.
           function ClassA(color) {
                this.color = color;
                this.SayColor = function () {
                    alert(this.color);
                }
            }
 
            function ClassB(colors, name) {
               //把ClassB的整个arguments对象作为第二个参数传递给apply方法
                ClassA.apply(this, arguments);
                this.name = name;
                this.SayName = function () {
                    alert(this.name);
                };
 
 
            }
            function ShowClass() {
                var colors = new ClassB("RGB(0,100,0)""绿色");
                colors.SayColor();
                colors.SayName();
            }
注:超类中的参数顺序与子类中的参数顺序一致时才可以传递参数对象,如果不是,就必须创建一个单独的数组,按照正确的顺序放置参数。此外,还可使用call()方法。



3.原型链继承方式
//确保构造函数中没有任何参数 原型链中标准做法
function ClassA() { 
}
        ClassA.prototype.color = "red";
        ClassA.prototype.SayColor = function () {
             alert(this.color);
        }
 
        function ClassB(name) {
 
            this.name = name;
            this.SayName = function () {
                alert(this.name);
            }
        }
//把ClassA的所有属性和方法 一次性赋给ClassB的prototype属性         ClassB.prototype = new ClassA();         function ShowClass() {             var Bsay = new ClassB("红色");             Bsay.SayColor();             Bsay.SayName();         }

注:1.子类的所有属性和方法都必须出现在子类的prototype属性被赋值后,添加的新方法会把原来的prototype属性覆盖掉。
2.原型链不支持多重继承,因为会用另一超类型的对象重写子类的prototype属性。



4.混合方式:对象冒充继承构造函数的属性,原型链继承prototype对象的方法(prototype对象是个模版,属于类的一个对象,其任何属性和方法都传递给该类的实例)

        //由于原型链方法构造函数不能带有参数,所以用混合型
        function ClassA(color) {
            this.color = color;
        }
        ClassA.prototype.SayColor = function () {
            alert(this.color);
        }
 
        function ClassB(Bcolor,name) {
            //对象冒充继承ClassA的属性
            this.name = name;
            ClassA.call(this, Bcolor);
        }
        //原型链继承ClassA超类的方法
        ClassB.prototype = new ClassA();
 
        //若SayName在继承超类的方法之前,则SayName会被覆盖掉(即子类的方法不能在继承其超类的语句之前)
        ClassB.prototype.SayName = function () {
            alert(this.name);
        }
        function ShowClass() {
            var Bsay = new ClassB("RGB(0,0,0)","红色");
            Bsay.SayColor();
            Bsay.SayName();
        }



posted @ 2012-07-11 14:58  bfy  阅读(323)  评论(0编辑  收藏  举报