码迷,mamicode.com
首页 > Web开发 > 详细

JS继承的实现方式

时间:2018-03-08 03:01:13      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:cti   on()   class   animal   混合   方法   ons   sha   ima   

1、继承第一种方式:对象冒充 

2、继承第二种方式:call()方法方式

3、继承的第三种方式:apply()方法方式

4、继承的第四种方式:原型链方式

5、继承的第五种方式:混合方式 


// 定义一个动物类 function Animal (name) { // 属性 this.name = name || ‘Animal‘; // 实例方法 this.sleep = function(){ console.log(this.name + ‘正在睡觉!‘); } } // 原型方法 Animal.prototype.eat = function(food) { console.log(this.name + ‘正在吃:‘ + food); }; function Cat(){ } Cat.prototype = new Animal(); Cat.prototype.name = ‘cat‘; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.eat(‘fish‘)); console.log(cat.sleep()); console.log(cat instanceof Animal); //true console.log(cat instanceof Cat); //true

  

JS继承的实现方式

标签:cti   on()   class   animal   混合   方法   ons   sha   ima   

原文地址:https://www.cnblogs.com/fm060/p/8525713.html

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