国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js的三種繼承方式詳解

2019-11-19 17:50:37
字體:
來源:轉載
供稿:網友

1.js原型(prototype)實現繼承

代碼如下

<body> <script type="text/javascript">  function Parent(name,age){   this.name=name;   this.age=age;   this.sayHi=function(){    alert("Hi, my name is "+this.name+", my age is "+this.age);   }  }//Child繼承Parent  function Child(grade){   this.grade=grade;   this.sayGrade=function(){    alert("My grade is "+this.grade);   }  }  Child.prototype=new Parent("小明","10");///////////   var chi=new Child("5");  chi.sayHi();  chi.sayGrade();</script> </body> 

2.構造函數實現繼承 

代碼如下:

<body> <script type="text/javascript">  function Parent(name,age){   this.name=name;   this.age=age;   this.sayHi=function(){    alert("Hi, my name is "+this.name+", my age is "+this.age);   }  }//Child繼承Parent   function Child(name,age,grade){   this.grade=grade;   this.sayHi=Parent;///////////   this.sayHi(name,age);   this.sayGrade=function(){    alert("My grade is "+this.grade);   }  }  var chi=new Child("小明","10","5");  chi.sayHi();  chi.sayGrade();</script> </body> 

3.call , apply實現繼承         -----很方便!

代碼如下:

<body> <script type="text/javascript">  function Parent(name,age){   this.name=name;   this.age=age;   this.sayHi=function(){    alert("Hi, my name is "+this.name+", my age is "+this.age);   }  }  function Child(name,age,grade){   this.grade=grade;   // Parent.call(this,name,age);///////////    // Parent.apply(this,[name,age]);/////////// 都可   Parent.apply(this,arguments);///////////    this.sayGrade=function(){    alert("My grade is "+this.grade);   }  // this.sayHi=function(){   //  alert("Hi, my name is "+this.name+", my age is "+this.age+",My grade is "+this.grade);   // }  }  var chi=new Child("小明","10","5");  chi.sayHi();  chi.sayGrade();</script> </body> 

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潜江市| 舞阳县| 建平县| 武定县| 海南省| 乃东县| 凤凰县| 旬邑县| 祥云县| 海晏县| 乐清市| 万州区| 仁寿县| 神农架林区| 邯郸县| 通化县| 全州县| 日照市| 石家庄市| 泰顺县| 息烽县| 漯河市| 汕尾市| 仲巴县| 贵德县| 北辰区| 永定县| 乐昌市| 福安市| 鹤壁市| 石屏县| 宁远县| 小金县| 漠河县| 隆尧县| 易门县| 崇义县| 孟村| 双城市| 镇康县| 北碚区|