一、原型鏈基礎
JavaScript的原型鏈是一種從對象到另一個對象的委託關係。它在一個對象的屬性找不到時,會沿着原型鏈往上找指定名稱的屬性或方法。我們來看一個例子:
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name){ this.name = name; } Dog.prototype = new Animal(); Dog.prototype.constructor = Dog; var dog = new Dog('Henry'); console.log(dog.getName()); //輸出:Henry
在此代碼中,我們定義了Animal和Dog兩個構造函數,並且讓Dog的原型指向Animal的實例。當我們調用dog.getName()時,JavaScript引擎會在dog對象上查找getName方法,沒有找到時會沿着原型鏈往上找Animal.prototype上的getName方法。
二、改變原型鏈
在上一個例子中,我們讓Dog的原型指向了Animal的實例,在Dog.prototype上定義getName方法。這種方式同樣有一些問題:
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name){ this.name = name; } Dog.prototype = new Animal(); Dog.prototype.constructor = Dog; Dog.prototype.getName = function(){ return 'My name is ' + this.name; } var dog = new Dog('Henry'); console.log(dog.getName()); //輸出:My name is Henry var animal = new Animal('Oscar'); console.log(animal.getName()); //輸出:Oscar
在此代碼中,我們在Dog.prototype上定義了一個新的getName方法,這會影響到它的父對象Animal.prototype上的getName方法,而且animal對象也會受到影響。
為了避免這種相互污染的問題,我們可以使用Object.create()方法,創建一個新的對象作為原型,並將其賦值給Dog.prototype。
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name){ this.name = name; } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.getName = function(){ return 'My name is ' + this.name; } var dog = new Dog('Henry'); console.log(dog.getName()); //輸出:My name is Henry var animal = new Animal('Oscar'); console.log(animal.getName()); //輸出:Oscar
三、繼承的多種方式
除了使用原型鏈實現繼承之外,我們還可以使用其他幾種方式:構造函數、組合繼承、寄生構造函數和組合繼承。
1. 構造函數
構造函數是最基本的一種繼承方式。它有一些局限性,例如無法訪問父對象的屬性和方法,但在一些情況下,由於其簡單易用,還是有着很大的用武之地。
function Animal(name){ this.name = name; this.getName = function(){ return this.name; } } function Dog(name, type){ this.type = type; this.getType = function(){ return this.type; } Animal.call(this, name); } var dog = new Dog('Henry', 'Poodle'); console.log(dog.getName()); //輸出:Henry console.log(dog.getType()); //輸出:Poodle
2. 組合繼承
組合繼承是指同時採用構造函數和原型鏈的繼承方式。使用該方法,可以避免構造函數和原型鏈各自的局限性和缺陷。
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name, type){ this.type = type; Animal.call(this, name); } Dog.prototype = new Animal(); Dog.prototype.constructor = Dog; Dog.prototype.getType = function(){ return this.type; } var dog = new Dog('Henry', 'Poodle'); console.log(dog.getName()); //輸出:Henry console.log(dog.getType()); //輸出:Poodle
3. 寄生構造函數
寄生構造函數是指在另一個構造函數的基礎上增加一些方法或屬性,從而達到繼承的目的。與傳統的構造函數繼承相比,寄生構造函數繼承的優勢在於可以使用閉包,對屬性和方法進行封裝,從而達到不污染父對象的目的。
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name, type){ var self = new Animal(name); self.type = type; self.getType = function(){ return this.type; } return self; } var dog = new Dog('Henry', 'Poodle'); console.log(dog.getName()); //輸出:Henry console.log(dog.getType()); //輸出:Poodle
4. 組合繼承
寄生組合繼承也是一種很好的繼承方式,它通過借用構造函數繼承父對象的屬性和方法,和通過原型鏈繼承父對象的原型,達到繼承的目的。
function Animal(name){ this.name = name; } Animal.prototype.getName = function(){ return this.name; } function Dog(name, type){ Animal.call(this, name); this.type = type; } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.getType = function(){ return this.type; } var dog = new Dog('Henry', 'Poodle'); console.log(dog.getName()); //輸出:Henry console.log(dog.getType()); //輸出:Poodle
原創文章,作者:TEFJL,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/368435.html