javascript中关于继承的例子

    技术2026-05-11  3

    <script> /**该例子用于动态模仿继承*/

     //定义父类 function Farent(name,age) {  this.name = name;  this.age  = age;  this.say  = function ()  {   alert("name:"+this.name + "~~age:"+this.age);  } }

     //定义子类 关键方法call function Child(name,age,work) {  Farent.call(this,name,age);  this.work = work;  //定义相同方法,就会覆盖父类Farent方法  this.say = function()  {   alert("name:"+this.name + "~~age:"+this.age+"~~work:"+this.work);  } }

     var farent = new Farent("parent",27); var child  = new Child("child",1,'desi'); farent.say(); child.say();

     

     </script>

    最新回复(0)