方法:1、利用原型讓一個引用型別繼承另外一個引用型別的屬性與方法;2、借用建構函數,在子類別建構子的內部呼叫超類別建構函數,藉由用call()和apply()可在新建立的物件上執行建構函式;3、將原型鍊和借用建構函式的技術組合在一塊實現繼承。
本教學操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。
前言:大多OO語言都支援兩種繼承方式: 介面繼承與實作繼承,而ECMAScript中無法實作介面繼承,ECMAScript只支援實作繼承,實作繼承主要是靠原型鏈來實現。
1.原型鏈
基本概念:利用原型讓一個引用型別繼承另一個引用型別的屬性和方法。
建構函數,原型,實例之間的關係:每個建構函數都有一個原型對象,原型對象包含一個指向建構函數的指針,而實例都包含一個指向原型對象的內部指針。
原型鏈實作繼承範例:
function SuperType() { this .property = true ; } SuperType.prototype.getSuperValue = function () { return this .property; } function subType() { this .property = false ; } //继承了SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function (){ return this .property; } var instance = new SubType(); console.log(instance.getSuperValue()); //true
#2.借用建構子
基本想法:在子型別建構函式的內部調用超類別建構函數,透過使用call()和apply()方法可以在新建立的物件上執行建構函數。
範例:
function SuperType() { this .colors = [ "red" , "blue" , "green" ]; } function SubType() { SuperType.call( this ); //继承了SuperType } var instance1 = new SubType(); instance1.colors.push( "black" ); console.log(instance1.colors); //"red","blue","green","black" var instance2 = new SubType(); console.log(instance2.colors); //"red","blue","green"
3.組合繼承
#基本概念:將原型鍊和借用建構函式的技術組合在一塊,從而發揮兩者之長的一種繼承模式。
範例:
function SuperType(name) { this .name = name; this .colors = [ "red" , "blue" , "green" ]; } SuperType.prototype.sayName = function () { console.log( this .name); } function SubType(name, age) { SuperType.call( this ,name); //继承属性 this .age = age; } //继承方法 SubType.prototype = new SuperType(); Subtype.prototype.constructor = Subtype; Subtype.prototype.sayAge = function () { console.log( this .age); } var instance1 = new SubType( "EvanChen" ,18); instance1.colors.push( "black" ); consol.log(instance1.colors); //"red","blue","green","black" instance1.sayName(); //"EvanChen" instance1.sayAge(); //18 var instance2 = new SubType( "EvanChen666" ,20); console.log(instance2.colors); //"red","blue","green" instance2.sayName(); //"EvanChen666" instance2.sayAge(); //20
4.原型式繼承
基本想法:借助原型可以基於已有的物件建立新對象,同時也不必須因此建立自訂的類型。
原型式繼承的想法可用以下函數來說明:
function object(o) { function F(){} F.prototype = o; return new F(); }
範例:
var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = object(person); anotherPerson.name = "Greg" ; anotherPerson.friends.push( "Rob" ); var yetAnotherPerson = object(person); yetAnotherPerson.name = "Linda" ; yetAnotherPerson.friends.push( "Barbie" ); console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
ECMAScript5透過新增Object.create()方法規範化了原型式繼承,這個方法接收兩個參數:一個用作新物件原型的物件和一個作為新物件定義額外屬性的物件。
var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = Object.create(person); anotherPerson.name = "Greg" ; anotherPerson.friends.push( "Rob" ); var yetAnotherPerson = Object.create(person); yetAnotherPerson.name = "Linda" ; yetAnotherPerson.friends.push( "Barbie" ); console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
5.寄生式繼承
基本概念:建立一個僅用於封裝繼承過程的函數,該函數在內部以某種方式來增強對象,最後再像真正是它做了所有工作一樣返回對象。
範例:
function createAnother(original) { var clone = object(original); clone.sayHi = function () { alert( "hi" ); }; return clone; } var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = createAnother(person); anotherPerson.sayHi(); ///"hi"
6.寄生組合式繼承
#基本思想:藉由借用函數來繼承屬性,透過原型鏈的混成形式來繼承方法
其基本模型如下所示:
function inheritProperty(subType, superType) { var prototype = object(superType.prototype); //创建对象 prototype.constructor = subType; //增强对象 subType.prototype = prototype; //指定对象 }
範例:
function SuperType(name){ this .name = name; this .colors = [ "red" , "blue" , "green" ]; } SuperType.prototype.sayName = function (){ alert( this .name); }; function SubType(name,age){ SuperType.call( this ,name); this .age = age; } inheritProperty(SubType,SuperType); SubType.prototype.sayAge = function () { alert( this .age); }
【推薦學習:javascript高階教學 】
以上是javascript是如何實作繼承的詳細內容。更多資訊請關注PHP中文網其他相關文章!

TonavigateReact'scomplexecosystemeffectively,understandthetoolsandlibraries,recognizetheirstrengthsandweaknesses,andintegratethemtoenhancedevelopment.StartwithcoreReactconceptsanduseState,thengraduallyintroducemorecomplexsolutionslikeReduxorMobXasnee

RectuseSkeyStoeficelyListifyListIdifyListItemsbyProvidistableIdentityToeachelement.1)keysallowReaeActTotRackChangEsInListSwithouterSwithoutreThoutreTheenteringTheEntirelist.2)selectuniqueandstablekeys,避免使用

KeysinrectarecrucialforOptimizingTherEnderingProcessandManagingDynamicListSefectefection.tospotaTandFixKey與依賴的人:1)adduniqueKeykeystoliquekeystolistItemStoAvoidWarningSwarningSwarningSwarningSperformance和2)useuniqueIdentifiersIdentifiersIdentifiersIdentifiersFromdatainSteAtofIndicessuessuessessemessuessessemessemessemesseysemessekeys,3)

React的單向數據綁定確保數據從父組件流向子組件。 1)數據流向單一,父組件狀態變化可傳遞給子組件,但子組件不能直接影響父組件狀態。 2)這種方法提高了數據流的可預測性,簡化了調試和測試。 3)通過使用受控組件和上下文,可以在保持單向數據流的同時處理用戶交互和組件間通信。

KeysinReactarecrucialforefficientDOMupdatesandreconciliation.1)Choosestable,unique,andmeaningfulkeys,likeitemIDs.2)Fornestedlists,useuniquekeysateachlevel.3)Avoidusingarrayindicesorgeneratingkeysdynamicallytopreventperformanceissues.

USESTATE()ISCICIALFOROPTIMINECREACTAPPPERFORMACTACEUTOPACTONCACTONRE REDERSANDUPDATES.TOOPTIMIZE:1)USEUSECALLBACKTOMEMOEMOEIZEFUNCTIONSANDPREVENTUNNNNNNNNNNNNNNNNENESMARYRERER.2)limemememememoforcachingExpensiveComputations.3)

使用Context和useState共享狀態是因為它們可以簡化大型React應用中的狀態管理。 1)減少propdrilling,2)代碼更清晰,3)更易管理全局狀態。但要注意性能開銷和調試複雜性,合理使用Context和優化技術可以提升應用的效率和可維護性。

使用不正確的鍵會導致React應用程序中的性能問題和意外行為。 1)鍵是列表項的唯一標識符,幫助React高效地更新虛擬DOM。 2)使用相同或不唯一的鍵會導致列表項重新排序和組件狀態丟失。 3)使用穩定且唯一的標識符作為鍵可以優化性能,避免全量重渲染。 4)使用工具如ESLint來驗證鍵的正確性。正確使用鍵可以確保React應用的高效和可靠性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中