首页 > 代码库 > JavaScript面向对象原型继承

JavaScript面向对象原型继承

	<script type="text/javascript">
		   //原型链继承

       function shape() {
           this.name="shape";
           this.showname=function(){
               console.log(this.name);
           }
         
       }

       function shape_Two() {
          this.name=‘shape_Two‘
       }

       function Triangle(side,height) {
          this.name="Triangle";
          this.side=side;
          this.height=height;
          this.getInfo=function(){
               console.log(this.side * this.height /2 );
            }
          }

          shape_Two.prototype=new shape();
          Triangle.prototype=new shape_Two();
          shape_Two.prototype.constructor=shape_Two;
          Triangle.prototype.constructor=Triangle;
          var mytri= new Triangle(10,8);
          console.log(mytri.name);
          mytri.getInfo();
          mytri.showname();
          console.log(mytri.constructor);
          console.log(mytri instanceof Triangle);
          console.log(mytri instanceof shape);
          console.log(mytri instanceof shape_Two);
          console.log(mytri instanceof Array);

          var mytri_2=new shape_Two();
              mytri_2.showname();
          var mytri_3= new shape();
         console.log(mytri_3.constructor);

         function shape_pro() {
            shape_pro.prototype.name=‘shape_pro‘;
            shape_pro.prototype.showName=function () {
                 console.log(this.name);
            }
         }

         function shape_Twopro( ) {  };
         shape_Twopro.prototype=new shape_pro();
         shape_Twopro.prototype.constructor=shape_Twopro;
         shape_pro.prototype.name="shape_Twopro";
       
        function Trianglepro(side,height){
            this.side=side;
            this.height=height;
        }
        Trianglepro.prototype=new shape_Twopro();
        Trianglepro.prototype.constructor=Trianglepro;
        Trianglepro.prototype.name="Trianglepro";
        Trianglepro.prototype.getArea=function() {
            console.log(this.side*this.height /2);
        }

        var  newmytri= new Trianglepro(12,8);
                 newmytri.showName();
                 newmytri.getArea(   );
                 console.time(newmytri.getArea());


      //临时构造器new F();
      

        function shape_snap() {
            shape_snap.prototype.name="snap";
            shape_snap.prototype.showName=function() {
                  console.log(this.name);
              }  
            }
           function shape_snapTwo(){};
           var newF =function ( ) {  }; 
            newF.prototype=shape_snap.prototype;
            shape_snapTwo.prototype=new newF();
            shape_snapTwo.prototype.constructor=shape_snapTwo;
            shape_snapTwo.prototype.name=‘shape_snapTwo‘;  
             
           function Triangleshape(side,height) {
                 this.side=side;
                 this.height=height;
             }
              var newF =function ( ) {  }; 
              newF.prototype=shape_snapTwo.prototype;
              Triangleshape.prototype=new newF();
              Triangleshape.prototype.constructor=Triangleshape;
              Triangleshape.prototype.name=‘Triangleshape‘;
              Triangleshape.prototype.getArea=function ( ) {
                   console.log(this.side*this.height/2);
              }

              var mytrinew=new Triangleshape(10,20);
               mytrinew.getArea();
              var sname= new shape_snap(); 
               sname.showName();

           //子对象访问父对象uber
           
             function shape_uber() {
                 shape_uber.prototype.name="shape_uber";
                 shape_uber.prototype.showuberName=function () {
                     var result =[];
                     if(this.constructor.uber){
                        result[result.length] =this.constructor.uber.showuberName();
                     }
                      result[result.length]=this.name;
                      return result.join(‘, ‘);
                 }
             };

              function shape_uberTwo() {  };

              var newf=function( ) {  };

              newf.prototype=shape_uber.prototype;
              shape_uberTwo.prototype=new newf();
              shape_uberTwo.prototype.constructor=shape_uberTwo;
              shape_uberTwo.uber=shape_uber.prototype;
              shape_uberTwo.prototype.name="shape_uberTwo";


              function shape_uberTriangle(side,height){
                  this.side=side;
                  this.height=height;
              }
              var newf=function ( ) { };
              newf.prototype=shape_uberTwo.prototype;
             shape_uberTriangle.prototype=new newf();
             shape_uberTriangle.prototype.constructor=shape_uberTriangle;
             shape_uberTriangle.uber=shape_uberTwo.prototype;
             shape_uberTriangle.prototype.name=‘shape_uberTriangle‘;
             shape_uberTriangle.prototype.getArea=function( ) {
                 console.log(this.side*this.height/2);
             }

               var mytridd= new shape_uberTwo();
             
               
             



</script>

  

JavaScript面向对象原型继承