摘要:借用構造函數借助或使用構造函數缺少函數復用原型鏈繼承使用指向對象子類無法給父類傳遞參數無法使用字面量添加新方法所有子類對象共享父類所有方法和屬性引用類型組合繼承借用構造函數對實例屬性繼承對共享方法繼承會調用兩次父類構造函數繼承
借用構造函數,借助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);
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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 與事件驅動給服務器帶來極高...
閱讀 3380·2021-11-22 09:34
閱讀 650·2021-11-19 11:29
閱讀 1350·2019-08-30 15:43
閱讀 2232·2019-08-30 14:24
閱讀 1867·2019-08-29 17:31
閱讀 1223·2019-08-29 17:17
閱讀 2617·2019-08-29 15:38
閱讀 2729·2019-08-26 12:10