JavaScript物件導向原型繼承

來源:互聯網
上載者:User

標籤:join   訪問   class   on()   new t   javascrip   array   物件導向   turn   

<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物件導向原型繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.