问题

比如我们有一个“动物”对象的构造函数。

	function animal() {
this.type = '动物';
}

还有一个“猫”对象的构造函数。

	function cat(name,color) {
this.name = name;
this.color = color;
}

我们知道猫也属于动物,如果这个猫对象想要继承动物对象的属性,我们该怎么做呢?

构造函数绑定

使用构造函数绑定是最简单的方法,使用call或者apply将父对象绑定在自对象上就可以了。

	function cat(name,color) {
animal.apply(this,arguments);
this.name = name;
this.color = color;
}
var cat1 = new cat("haha", 'red');
console.log(cat1.type); //动物

不过这种方法比较少见。

拷贝继承

如果把父对象的所有属性和方法,拷贝进子对象,也可以实现继承。

	function extend(Child, Parent) {
    var p = Parent.prototype;
    var c = Child.prototype;
    for (var i in p) {
      c[i] = p[i];
      }
    c.uber = p; //桥梁作用
  }

使用方法:

	extend(cat, animal);
var cat1 = new cat("haha","red");
alert(cat1.type); // 动物

原型继承(prototype)

相比于上面的直接绑定,原型继承的方法比较常见,对于prototype,我自己简单总结了一下。

每个函数都有一个prototype属性,这个属性是指向一个对象的引用,当使用new关键字创建新实例的时候,这个实例对象会从原型对象上继承属性和方法。

也就是说,如果将“猫”构造函数的prototype属性指向一个“动物”实例,那么再创建“猫”对象实例的时候,就继承了“动物”对象的属性和方法了。

继承实例

	cat.prototype = new animal();
cat.prototype.constructor = cat;
var cat1 = new cat("haha","red");
console.log(cat1.constructor == cat); //true
console.log(cat1.type); // 动物
  • 代码第一行,我们将cat函数的prototype对象指向一个animal对象的实例(其中就包含了animal的type属性了)。

  • 代码第二行是什么意思呢?

    • 首先,假如我们没有加这行代码,运行

      	cat.prototype = new animal();
      console.log(cat.prototype.constructor == animal); //true

      也就是说,其实每个prototype对象都有一个constructor属性,指向它的构造函数。

    • 我们再看下面的代码

      	cat.prototype = new animal();
      var cat1 = new cat("haha", 'red');
      console.log(cat1.constructor == animal); //true

      由上我们看到实例cat1的构造函数是animal,所以,显然是不对的。。。cat1明明是new cat()才生成的,所以我们应该手动纠正。cat.prototype对象的constructor值改为cat。

    • 所以这也是我们应该注意的一点,如果我们替换了prototype对象,就应该手动纠正prototype对象的constructor属性。

      	o.prototype = {};
      o.prototype.constructor = o;

直接继承prototype

由于在animal对象中,不变的属性可以直接写在animal.prototype中。然后直接让cat.prototype指向animal.prototype也就实现了继承。

现在我们先将animal对象改写成:

	function animal() {
}
animal.prototype.type = '动物';

然后再实现继承:

	cat.prototype = animal.prototype;
cat.prototype.constructor = cat;
var cat1 = new cat("haha","red");
console.log(cat1.type); // 动物

与上一种方法相比,这种方法显得效率更高(没有创建animal实例),节省了空间。但是这样做正确吗?答案是不正确,我们继续看。

	cat.prototype = animal.prototype;

这行代码让cat.prototype和animal.prototype指向了同一个对象,所以如果改变了cat.prototype的某一个属性,都会反映到animal.prototype上,这显然不是我们想要看到的。

比如我们运行:

	console.log(animal.prototype.constructor == animal)  //false

结果看到是false,为什么呢?cat.prototype.constructor = cat;这一行就会把animal.prototype的constructor属性也改掉了。

利用空对象作为中介

	var F = function(){};
F.prototype = animal.prototype;
cat.prototype = new F();
cat.prototype.constructor = cat;

结合上面两种方法,因为F是空对象,所以几乎不占内存。这时修改cat的prototype对象,就不会影响到animal的prototype对象。

	console.log(animal.prototype.constructor == animal);   // true

然后我们将上面的方法封装一下:

	function extend(Child, Parent) {
    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }

使用的时候,方法如下:

	extend(cat,animal);
var cat1 = new cat("haha","red");
console.log(cat1.type); // 动物

Child.uber = Parent.prototype; 这行代码就是个桥梁作用,让子对象的uber属性直接指向父对象的prototype属性,等于在自对象上打开一条叫uber的通道,让子对象的实例能够使用父对象的所有属性和方法。

最新文章

  1. JSONArray的应用
  2. zDiaLog弹出层
  3. C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试
  4. Visual Studio 当前上下文中不存在名称“ConfigurationManager”
  5. ActionScript 3.0 for the Lunder Algorithm
  6. Ruby求出数组中最小值及其下标
  7. mkimage工具 加载地址和入口地址 内核启动分析
  8. 安装Debian 7.8 过程,以及该系统的配置过程
  9. centos6.5 mysql安装+远程访问+备份恢复+基本操作+卸载
  10. 微信平台接入Web页面功能接口(C#)
  11. 多线程编程中使用pthread_create内存泄露问题
  12. python--代码统计小程序
  13. SQL Server 全文索引的管理
  14. Coco2dx制作一个3D旋转的效果
  15. 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)
  16. Storm是什么
  17. 【中间件安全】IIS7.0 安全加固规范
  18. Linux文件系统命令 pwd
  19. Android Locale
  20. 你也可以手绘二维码(二)纠错码字算法:数论基础及伽罗瓦域GF(2^8)

热门文章

  1. java中线程存活和线程执行的问题!
  2. java.lang.Long cannot be cast to java.lang.Integer解决办法
  3. AngularJS入门心得3——HTML的左右手指令
  4. JS魔法堂:彻底理解0.1 + 0.2 === 0.30000000000000004的背后
  5. Redhat7.2 如何修改主机名(hostname)?
  6. [Offer收割]编程练习赛3 - 题目3 : 智力竞赛
  7. Dapper学习 - Dapper.Rainbow(一) - Create
  8. Emit学习(2) - IL - 常用指令介绍
  9. ASP.NET MVC处理JsonResult返回时间DateTime问题
  10. 呼叫外部js文件并使用其内部方法