分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

js 继承

发布时间:2023-09-06 01:53责任编辑:董明明关键词:js
/** * 组合继承 */function Super(name) { ???this.name = name || ‘‘;}Super.prototype.age = 23;function Sub(name) { ???Super.call(this, name); ???this.eat = function() { ???????return this.name + ‘eat‘; ???}}Sub.prototype = new Super();Sub.prototype.constructor = Subvar sub = new Sub(‘xiaoming‘);console.log(sub);

  

/** * 寄生组合继承 */function Super(name) { ???this.name = name || ‘‘;}Super.prototype.age = 23;function init(Sub, Sup) { ???var proto = Object(Sup.prototype); ???proto.constructor = Sub; ???Sub.prototype = proto;}function Sub(name) { ???Super.call(this, name); ???this.eat = function() { ???????return this.name + ‘eat‘; ???}}init(Sub, Super);// Sub.prototype = new Super();// Sub.prototype.constructor = Subvar sub = new Sub(‘xiaoming‘);console.log(sub);

  

1、原型链继承

2、构造函数继承

3、组合继承

  其实就是结合了原型链继承和构造函数的继承

  缺点:之类继承父类的过程需要两次调用父类的构造器,这其实很浪费。

4、组合寄生继承

  优点:解决了组合继承的缺点

  缺点:复杂,而且当你改变子类的prototype时,父类的prototype也会改变。

js 继承

原文地址:https://www.cnblogs.com/pengwenfan/p/9012913.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved