欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂JavaScript實現(xiàn)面向?qū)ο罄^承五種方式小詳解

2018-07-23 14:30:33 1151瀏覽

今天扣丁學堂小編給大家整理了一篇關于Java培訓之JS實現(xiàn)面向?qū)ο罄^承方式的詳解,首先js是門靈活的語言,實現(xiàn)一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現(xiàn)的,根據(jù)js語言的本身的特性,js實現(xiàn)繼承有以下通用的幾種方式:



1.使用對象冒充實現(xiàn)繼承(該種實現(xiàn)方式可以實現(xiàn)多繼承)

實現(xiàn)原理:讓父類的構造函數(shù)成為子類的方法,然后調(diào)用該子類的方法,通過this關鍵字給所有的屬性和方法賦值

  functionParent(firstname)
  {
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
  console.log(this.age);
  }
  }
  functionChild(firstname)
  {
  this.parent=Parent;
  this.parent(firstname);
  deletethis.parent;
  this.saySomeThing=function()
  {
  console.log(this.fname);
  this.sayAge();
  }
  }
  varmychild=newChild("李");
  mychild.saySomeThing();

2.采用call方法改變函數(shù)上下文實現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)

實現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this,使它指向傳入函數(shù)的具體對象

  functionParent(firstname)
  {
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
  console.log(this.age);
  }
  }
  functionChild(firstname)
  {
  this.saySomeThing=function()
  {
  console.log(this.fname);
  this.sayAge();
  }
  this.getName=function()
  {
  returnfirstname;
  }
  }
  varchild=newChild("張");
  Parent.call(child,child.getName());
  child.saySomeThing();

3.采用Apply方法改變函數(shù)上下文實現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)

實現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this,使它指向傳入函數(shù)的具體對象

  functionParent(firstname)
  {
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
  console.log(this.age);
  }
  }
  functionChild(firstname)
  {
  this.saySomeThing=function()
  {
  console.log(this.fname);
  this.sayAge();
  }
  this.getName=function()
  {
  returnfirstname;
  }
  }
  varchild=newChild("張");
  Parent.apply(child,[child.getName()]);
  child.saySomeThing();

4.采用原型鏈的方式實現(xiàn)繼承

實現(xiàn)原理:使子類原型對象指向父類的實例以實現(xiàn)繼承,即重寫類的原型,弊端是不能直接實現(xiàn)多繼承

  functionParent()
  {
  this.sayAge=function()
  {
  console.log(this.age);
  }
  }
  functionChild(firstname)
  {
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
  console.log(this.fname);
  this.sayAge();
  }
  }
  Child.prototype=newParent();
  varchild=newChild("張");
  child.saySomeThing();

5.采用混合模式實現(xiàn)繼承

  functionParent()
  {
  this.sayAge=function()
  {
  console.log(this.age);
  }
  }
  Parent.prototype.sayParent=function()
  {
  alert("thisisparentmethod!!!");
  }
  functionChild(firstname)
  {
  Parent.call(this);
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
  console.log(this.fname);
  this.sayAge();
  }
  }
  Child.prototype=newParent();
  varchild=newChild("張");
  child.saySomeThing();
  child.sayParent();

以上就關于扣丁學堂JavaScript實現(xiàn)面向?qū)ο罄^承方式的詳細介紹,希望對大家有所幫助,最后想要了解更多內(nèi)容的小伙伴可以登錄扣丁學堂官網(wǎng)咨詢??鄱W堂不僅有專業(yè)的老師和與時俱進的課程體系,還有大量的Java視頻教程供學員觀看學習,想要快速學習Java開發(fā)技術的小伙伴快快行動吧。

扣丁學堂微信公眾號



【關注微信公眾號獲取更多學習資料】



查看更多關于“Java開發(fā)資訊”的相關文章>>

標簽: Java培訓 Java開發(fā)程序員 Java視頻教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部