摘要:讓子的原型鏈指向父的實(shí)例父實(shí)例化的對象讓父的屬性創(chuàng)建在子的上把的方法應(yīng)用到這個(gè)對象身上傳遞一個(gè)數(shù)組或,而就要一個(gè)個(gè)地傳過案例中的繼承在中實(shí)現(xiàn)繼承等價(jià)于
繼承 call實(shí)現(xiàn)繼承
用于實(shí)現(xiàn)函數(shù)繼承,call的第二個(gè)參數(shù)可以是任意類型
function Animal(){ this.name = ""; this.say = function(){ console.log("hello"); } } function dog(){ this.name = "xiao"; //把Animal的方法應(yīng)用到dog這個(gè)對象身上 Animal.call(this); } console.log(new dog()); // dog { name: "", say: [Function] }apply實(shí)現(xiàn)繼承
用于實(shí)現(xiàn)函數(shù)繼承,apply的第二個(gè)參數(shù)必須是數(shù)組,也可以是arguments
function Animal(){ this.name = ""; this.say = function(){ console.log("hello"); } } function dog(){ this.name = "xiao"; //把Animal的方法應(yīng)用到dog這個(gè)對象身上 Animal.apply(this,arguments); } console.log(new dog()); // dog { name: "", say: [Function] }繼承的優(yōu)化
如果構(gòu)造函數(shù)this綁定太多屬性,在實(shí)例化后會造成浪費(fèi),為此我們一般會使用原型鏈來優(yōu)化,但是使用原型鏈之后我們的apply和call的繼承方法就會失效,因此我們一般使用混合的寫法。
讓子的原型鏈指向父的實(shí)例(父實(shí)例化的對象)
dog.prototype = new Animal();
讓父的屬性創(chuàng)建在子的this上
Animal.call(this)
function Animal(name){ this.name = name; this.say = function(){ console.log("hello"); } } Animal.prototype.action = function() { console.log("running"); } function dog(name,type){ this.name = name; //把Animal的方法應(yīng)用到dog這個(gè)對象身上 Animal.call(this,type); } dog.prototype = new Animal(); console.log(new dog("xiao", "gold")); // Animal { name: "gold", say: [Function] } (new dog("xiao")).action() //running
apply 傳遞一個(gè)數(shù)組或arguments,而call就要一個(gè)個(gè)地傳過
案例es5中的繼承
function Reactangle(length,width) { this.length = length; this.width = width; } Reactangle.prototype.getArea = function(){ return this.length * this.width; } function Square(length) { Reactangle.call(this.length,length); } Square.prototype = Object.create(Reactangle.prototype,{ constructor: { value:Square, enumerable:true, writeable:true, configurable:true } }); var square = new Square(3); console.log(square.getArea()); console.log(square instanceof Square); console.log(square instanceof Reactangle);
在es6中實(shí)現(xiàn)繼承
class Reactangle { constructor(length,width) { this.length = length; this.width = width; } getArea() { return this.length * this.width; } } class Square extends Reactangle { constructor(length) { // 等價(jià)于 Reactangle.call(this.length,length) super(length,length); } } var square = new Square(3); console.log(square.getArea()); // 9 console.log(square instanceof Square); // true
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/93029.html
摘要:前言在理想的狀態(tài)下,你可以在深入了解之前了解和開發(fā)的所有知識。繼承另一個(gè)類的類,通常稱為類或類,而正在擴(kuò)展的類稱為類或類。這種類型的組件稱為無狀態(tài)功能組件。在你有足夠的信心構(gòu)建用戶界面之后,最好學(xué)習(xí)。 原文地址:JavaScript Basics Before You Learn React 原文作者: Nathan Sebhastian 寫在前面 為了不浪費(fèi)大家的寶貴時(shí)間,在開...
摘要:原文鏈接恰當(dāng)?shù)貙W(xué)習(xí)適合第一次編程和非的程序員持續(xù)時(shí)間到周前提無需編程經(jīng)驗(yàn)繼續(xù)下面的課程。如果你沒有足夠的時(shí)間在周內(nèi)完成全部的章節(jié),學(xué)習(xí)時(shí)間盡力不要超過周。你還不是一個(gè)絕地武士,必須持續(xù)使用你最新學(xué)到的知識和技能,盡可能地經(jīng)常持續(xù)學(xué)習(xí)和提高。 原文鏈接:How to Learn JavaScript Properly 恰當(dāng)?shù)貙W(xué)習(xí) JavaScript (適合第一次編程和非 JavaSc...
摘要:過濾掉等于的數(shù)組元素返回一個(gè)新數(shù)組,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值數(shù)組元素平方用于檢測數(shù)組中的元素是否滿足指定條件如果有一個(gè)元素滿足條件,則表達(dá)式返回剩余的元素不會再執(zhí)行檢測如果沒有滿足條件的元素,則返回。 數(shù)組常用方法 創(chuàng)建數(shù)組 var fruits = [Apple, Banana]; console.log(fruits.length); 通過索引訪問數(shù)組元素 v...
摘要:類的繼承,說明了不同類直接的關(guān)系,派生類復(fù)用了基類的代碼同時(shí)也繼承了基類的屬性和方法。派生類的實(shí)例化會創(chuàng)建該類的一個(gè)新實(shí)例。派生類既可以單獨(dú)繼承一個(gè)基類,也可以多重繼承多個(gè)基類。 面向?qū)ο笳Z言的一個(gè)特性就是類的繼承。繼承的關(guān)系跟人類繁衍的關(guān)系相似,被繼承的類稱為基類(也叫做父類),繼承而得的類叫派生類(也叫子類),這種關(guān)系就像人類的父子關(guān)系。 showImg(https://segme...
閱讀 2164·2023-04-26 00:38
閱讀 1946·2021-09-07 10:17
閱讀 899·2021-09-02 15:41
閱讀 650·2021-08-30 09:45
閱讀 557·2019-08-29 17:25
閱讀 3227·2019-08-29 15:07
閱讀 2202·2019-08-29 12:52
閱讀 3748·2019-08-26 13:35