码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 面向对象继承的实现

时间:2015-05-05 18:17:51      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

 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>

 

JavaScript 面向对象继承的实现

标签:

原文地址:http://www.cnblogs.com/terry01/p/4479328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!