一直以来对Javascript的原型、原型链、继承等东西都只是会用和了解,但没有深入去理解这门语言关于继承这方面的本质和特点。闲暇之余做的理解和总结,欢迎各位朋友一起讨论。

本文本主要从两段代码的区别说明继承:

一、第一段代码:

function Parent(){
this.Name='parent';
}
Parent.prototype.getName = function(){
return this.Name;
}
Parent.prototype.Share = ["Share"]; function Child(){
Parent.call(this)
this.Age = 'Age'
}
Child.prototype = Parent.prototype; //原型继承
var _child = new Child();
var _parent = new Parent(); Parent.prototype.Foo = "Foo"; _parent.Share.push('second');
console.log(_child.Share);
console.log("_child instanceof Child: " + ( _child instanceof Child ));
console.log("_child instanceof Parent: " + ( _child instanceof Parent));
console.log("Child.prototype.isPrototypeOf(_child): " + Child.prototype.isPrototypeOf(_child));
console.log("Parent.prototype.isPrototypeOf(_child): " + Parent.prototype.isPrototypeOf(_child));
console.log("_child.hasOwnProperty('Share'): " + _child.hasOwnProperty('Share'));
console.log("_parent.hasOwnProperty('Share'): " + _parent.hasOwnProperty('Share'));
console.log("_child.hasOwnProperty('Name'): " + _child.hasOwnProperty('Name'));
console.log("_parent.hasOwnProperty('Name'): " + _parent.hasOwnProperty('Name'));

二、第二段代码:

function Parent(){
this.Name='parent';
}
Parent.prototype.getName = function(){
return this.Name;
}
Parent.prototype.Share = ["Share"]; function Child(){
Parent.call(this)
this.Age = 'Age'
} function Inher(supers, childs) {
function F(){}
F.prototype = supers.prototype;
F.prototype.constructor = childs;
return new F();
} Child.prototype = Inher(Parent,Child); //原型对象继承
var _child = new Child();
var _parent = new Parent();
_parent.Share.push('second'); Parent.prototype.Foo = "Foo"; console.log(_child.Share);
console.log("_child instanceof Child: " + (_child instanceof Child));
console.log("_child instanceof Parent: " + (_child instanceof Parent));
console.log("Child.prototype.isPrototypeOf(_child): " + Child.prototype.isPrototypeOf(_child));
console.log("Parent.prototype.isPrototypeOf(_child): " + Parent.prototype.isPrototypeOf(_child));
console.log("_child.hasOwnProperty('Share'): " + _child.hasOwnProperty('Share'));
console.log("_parent.hasOwnProperty('Share'): " + _parent.hasOwnProperty('Share'));
console.log("_child.hasOwnProperty('Name'): " + _child.hasOwnProperty('Name'));
console.log("_parent.hasOwnProperty('Name'): " + _parent.hasOwnProperty('Name'));

三、运行结果:

从结果可以看出两段代码的运行结果是一致的。

  • 对象是否是父类、子类的实例判断都是一致的
  • 父类、子类都是在对象的原型链上
  • prototype(原型)上的属性通过hasOwnProperty判断是false
  • 通过构造函数创建的属性,可以通过HasOwnProperty决断为true的

不同之处:

  • 代码不同之处在于:第一段代码Child.prototype是直接被赋值为Parent.prototype的,而第二段代码Child.prototype被赋值为一个Parent的实例对象
  • 上图说明:左侧为第一段代码;右侧为第二段
  • 从上图可以看出第一段代码的原型链只有两层,也就是Child和Parent共用一层原型链,第二段代码的原型链有三层,Child、Parent、Object。如果是多级继承,第一段代码的模式原始链始终只有两层,而第二段代码的模式原型会有层级关系。
    • 原因:function被实例化时,先创建一个对象,然后复制function的构造器的prototype属性上的所有内容到__proto__(后续的原型链),如果复制是一个对象,这个对象也有自己的原型链(proto),所以原型链就形成了。如果子类的prototype指向父类的prototype,这样prototype处于共存状态则原型链不清晰。
  • 重点区别(第二天的理解)!!: 第一段代码在子类的prototype(原型)上增加的方法或者属性会直接影响到父类;而第二段代码的则不会。

最新文章

  1. Android屏幕适配
  2. 【jQuery基础学习】06 jQuery表单验证插件-Validation
  3. the computer spends over 96% of its time waiting for I/O devices to finish transferring data
  4. 关于scrollTop
  5. 分享一个BUG
  6. 简话ASP.NET Web API
  7. SharePoint2013上传列表模板(后缀stp)
  8. Python常用数据结构之heapq模块
  9. 用bat文件启动mongodb
  10. APP测试流程的总结
  11. Android ContenObserver 监听联系人数据变化
  12. Findout之为什么公司内部不能使用SSH协议连接外网服务器
  13. mysql中的int和tinyint、varchar和char、DateTime和TimeStamp区别
  14. jquery中的 jquery.contains(a,b)
  15. Asp.Net 之 Web.config 配置文件详解
  16. ubuntu16.04英文版搜狗输入法安装报错
  17. Ubuntu 16.04安装sogou 拼音输入法
  18. rancher2 HA部署注意事项
  19. DataTables | Table plug-in for jQuery
  20. [Agc011F] Train Service Planning

热门文章

  1. pwn学习之四
  2. cmd命令中运行pytest命令导入模块报错解决方法
  3. 如何删除github上的某个文件夹
  4. [python]numpy.mean()用法
  5. layedit第三次改造
  6. Java 跨平台原理
  7. meta标签的用处详解
  8. Jquery weui picker 支持label和value
  9. 可道云kodexplorer网盘未清理造成linux服务器爆满的解决方法
  10. 20175324王陈峤宇 《Java程序设计》第六周学习总结