Javascript初级 JavaScript进阶

JavaScript多重继承


示例代码


<html>

<head>

    <meta charset=”utf-8>

    <title></title>

</head>

<body>

     <script type=”text/javascript”>

        Function object(o){

           Function  F(){};

           F.prototype=o;  

           Return new F();

     }

     Function object(o){

     }

     Function object(o){

          Function F(){};

F.prototype=o;

Var n=new F();

N.uber=o;

Return n;

}

 Var shape={

Name:’shape’,

Tostring:function(){

     Return this.name;

}

};

Var twodshape=object(shape);

Twodshape.name=’2d shape’;

Twodshape.tostring=function(){

Return this.uber.tostring()+’- -’+this.name;

}

Var triangle=object(Twodshape)

Triangle.name=’triangle’;

Triangle.getarea=function(){

Return this.side*this.height/2;

}

Console.log(triangle.tostring());

        </script>

</body>

</html>


执行结果


javascript1.png 

 

示例代码


<html>

<head>

    <meta charset=”utf-8>

    <title></title>

</head>

<body>

     <script type=”text/javascript”>

     Function objectplus(o,stuff){

        Function F(){};

        F.prototype=o;

        Var n=new F();

        N.uber=o;

        For(var i in stuff){

            n[i]=stuff[i];

        }

        Return n;

     }

Var shape={

     Name:’shape’,

     Tostring:function(){}

       Return this,name;

     }

};

Var twodshape=objectplus(shape,a{

   Name:’2d shape’,

   Tostring:function(){

        Return this.uber.tostring()+’- -’+this.name;

   }

});

Var Triangle=objectplus(TwoDShpe,{

Name :’triangle’,

getArea:funcyion(){

Return this.side*this.height/2;

},

Side:0

Height:0;

});

Var my=objectplus(triangle,{

   Side:5,

   Height:10

});

Console.log(my.getarea());

Console.log(my.tostring());

 

Var my=objectplus(triangle,{

Name:’this is a test’,

Side:10

Height:20

});

Console.log(my.tostring());

        </script>

</body>

</html>

 

执行结果


javascript2.png 

 


【本文由麦子学院独家原创,转载请注明出处并保留原文链接】

logo
© 2012-2016 www.maiziedu.com
蜀ICP备13014270号-4 Version 5.0.0 release20160127

您有一个双11钜惠红包未领取

客服热线 400-862-8862

回到顶部