摘要:借用构造函数借助或使用构造函数缺少函数复用原型链继承使用指向对象子类无法给父类传递参数无法使用字面量添加新方法所有子类对象共享父类所有方法和属性引用类型组合继承借用构造函数对实例属性继承对共享方法继承会调用两次父类构造函数继承
借用构造函数,借助call或apply,使用Superclass构造函数,缺少函数复用
function Superclass(name = "default") { this.name = name; } function Subclass(age = 22, name) { this.age = age; Superclass.call(this, name); this.say = function () { console.log(`this.name=${this.name}...this.age=${this.age}`); }; } const test = new Subclass(23); test.say(); console.log(`test instanceof Subclass`, test instanceof Subclass); console.log(`test instanceof Superclass`, test instanceof Superclass); console.log(test.__proto__.constructor); console.log(Subclass.prototype.constructor);
原型链继承,使用prototype指向Superclass对象,子类无法给父类传递参数,无法使用字面量添加新方法,所有子类对象共享父类所有方法和属性(引用类型)
function Superclass(name = "default") { this.name = name; this.colors = ["r", "y"]; } function Subclass(age = 22) { this.age = age; this.say = function () { console.log(`this.name=${this.name}...this.age=${this.age}`); }; } Subclass.prototype = new Superclass(); Subclass.prototype.constructor = Subclass; const test = new Subclass(23); const test1 = new Subclass(24); test.colors.push("b"); console.log(test.colors); console.log(test1.colors); test.say(); test1.say(); console.log(`test instanceof Subclass`, test instanceof Subclass); console.log(`test instanceof Superclass`, test instanceof Superclass); console.log(test.__proto__.constructor); console.log(Subclass.prototype.constructor);
组合继承,prototype+call/apply,借用构造函数对实例属性继承,prototype对共享方法继承,会调用两次父类构造函数
function Superclass(name = "default") { this.name = name; } Superclass.prototype.say = function () { console.log(this.name); }; function Subclass(age = 22, name) { this.age = age; Superclass.call(name); } Subclass.prototype = new Superclass(); Subclass.prototype.constructor = Subclass; const test = new Subclass(23); test.say(); console.log(`test instanceof Subclass`, test instanceof Subclass); console.log(`test instanceof Superclass`, test instanceof Superclass); console.log(test.__proto__.constructor); console.log(Subclass.prototype.constructor);
ES6 Class继承
class Superclass { constructor(name = "default") { this.name = name; } } class Subclass extends Superclass { constructor(age = 22, name) { super(name); this.age = age; } say() { console.log(`this.name=${this.name}...this.age=${this.age}`); } } const test = new Subclass(23); test.say(); console.log(`test instanceof Subclass`, test instanceof Subclass); console.log(`test instanceof Superclass`, test instanceof Superclass); console.log(test.__proto__.constructor); console.log(Subclass.prototype.constructor);
Node util.inherits;
function Superclass(name = "default") { this.name = name; } function Subclass(age = 22, name) { this.age = 22; Superclass.call(this, name); this.say = function () { console.log(`this.name=${this.name}...this.age=${this.age}`); }; } require("util").inherits(Subclass, Superclass); const test = new Subclass(23); test.say(); console.log(`test instanceof Subclass`, test instanceof Subclass); console.log(`test instanceof Superclass`, test instanceof Superclass); console.log(test.__proto__.constructor); console.log(Subclass.prototype.constructor);
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/81456.html
摘要:一实体类的定义定义类有参构造方法二定义方法以设置实体类的属性值方法三定义方法以获取实体类的属性值方法四构造实例对象使用全参构造方法获取实例对象桐人男控制台打印实例 一、Node.js 实体类 的定义 //定义类Person 有参构造方法 function Person(name, sex, age, addr, salary) { this.name = name; t...
摘要:为指定事件注册一个监听器,接受一个字符串和一个回调函数。发射事件,传递若干可选参数到事件监听器的参数表。为指定事件注册一个单次监听器,即监听器最多只会触发一次,触发后立刻解除该监听器。 1.Node.js 简介 Node.js 其实就是借助谷歌的 V8 引擎,将桌面端的 js 带到了服务器端,它的出现我将其归结为两点: V8 引擎的出色; js 异步 io 与事件驱动给服务器带来极高...
阅读 3326·2021-11-22 09:34
阅读 602·2021-11-19 11:29
阅读 1312·2019-08-30 15:43
阅读 2173·2019-08-30 14:24
阅读 1809·2019-08-29 17:31
阅读 1197·2019-08-29 17:17
阅读 2549·2019-08-29 15:38
阅读 2694·2019-08-26 12:10