Fork me on GitHub

JavaScript 面向对象继承的实现

 1 <script type="text/javascript">
 2  function Animal () {
 3      this.species="Animal";
 4  }
 5 function Cat(name,color){
 6     this.name=name;
 7     this.color=color;
 8 }
 9 //Apply 将父类对象的构造函数赋值到子类上,Animal.apply(this,arguments);
10  function Cat(name,color){
11      this.name=name;
12      this.color=color;
13      Animal.apply(this,arguments);
14  }
15  var cat1=new Cat("大毛","黄色");
16  alert(cat1.species);
17 
18 //Prototype 子对象原型prototype赋值父类对象,并将子类对象原型的构造函数赋值为子类
19 Cat.prototype = new Animal();
20 Cat.prototype.constructor=Cat;
21 var cat1=new Cat("大毛","黄色");
22 alert(cat1.species);
23 </script>

 

posted @ 2015-05-05 15:46  Terry√  阅读(142)  评论(0编辑  收藏  举报