亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

React Native填坑之旅--class(番外篇)

TwIStOy / 3094人閱讀

摘要:構(gòu)造函數(shù)定義偵探類作為例子。里的既是類的定義,也是構(gòu)造函數(shù)。在構(gòu)造函數(shù)中定義的實例方法和屬性在每一個實例中都會保留一份,而在原型中定義的實例方法和屬性是全部實例只有一份。

無論React還是RN都已經(jīng)邁入了ES6的時代,甚至憑借Babel的支持都進(jìn)入了ES7。ES6內(nèi)容很多,本文主要講解類相關(guān)的內(nèi)容。

構(gòu)造函數(shù)

定義偵探類作為例子。

ES5的“類”是如何定義的。

function ES5Detective() {
  console.log("##ES5Detective contructor");
}

ES6定義類:

class ES6Detective {
  constructor() {
    console.log("Detective constructor");
  }
}

ES6使用了class關(guān)鍵字,而且有專門的constructor。ES5里的function ES5Detective既是類的定義,也是構(gòu)造函數(shù)。

屬性

看看這個偵探是從哪本書出來的。

ES5:

ES5Detective.prototype.fromBookName = "who";

ES6:

class ES6Detective {
  detectiveName: string;
  _bookName: string;

  constructor() {
    console.log("Detective constructor");
    this.detectiveName = "Detective who"; // 屬性
  }
}
ES6 getter & setter
class ES6Detective {
  detectiveName: string;
  _bookName: string;

  constructor() {
    console.log("Detective constructor");
    this.detectiveName = "Detective who";
    this._bookName = "who";
  }

  get fromBookName() {
    return this._bookName;
  }

  set fromBookName(value) {
    this._bookName = value;
  }
}

如果只有g(shù)etter沒有setter而賦值的話就會出現(xiàn)下面的錯誤:

detective.bookAuthor = "A C";
                     ^

TypeError: Cannot set property bookAuthor of # which has only a getter
實例方法

偵探是如何解決案件的。

ES5:

ES5Detective.prototype.solveCase = function(caseName) {
  var dn = this.dectiveName;
  if(!caseName) {
    console.log("SOLVE CASE: " + dn + " no case to solve");
  } else {
    console.log("SOLVE CASE: " + dn + " get case " + caseName + " is solved");
  }
};

或者:

function ES5Detective() {
  this.dectiveName = "Detective who";
  console.log("##ES5Detective contructor");
  // 實例方法
  this.investigate = function(scene) {
    console.log("investigate " + scene);
  }

  this.assistant = "assistant who";
}

ES6:

class ES6Detective {
  detectiveName: string;
  _bookName: string;

  constructor() {
    console.log("Detective constructor");
    this.detectiveName = "Detective who";
    this._bookName = "who";
  }

  solveCase(caseName) {
    if(!caseName) {
      console.log("no case to solve");
    } else {
      console.log("case " + caseName + " is solved");
    }
  }
}

ES6添加方法非常簡單直接。ES5中添加實例方法有兩種方法,一是在prototype里定義,一是在構(gòu)造函數(shù)重定義。在構(gòu)造函數(shù)中定義的實例方法和屬性在每一個實例中都會保留一份,而在原型中定義的實例方法和屬性是全部實例只有一份

另外,在ES5的構(gòu)造函數(shù)重定義的實例方法可以訪問類的私有變量。比如:

function ES5Detective() {
  console.log("##ES5Detective contructor");

  var available: boolean = true; // private field. default income is ZERO.
  this.investigate = function(scene) {
    if (available) {
      console.log("investigate " + scene);
    } else {
      console.log(`i"m not available`);
    }
  }
}

在其他的方法訪問的時候就會報錯。

if (!available) {
     ^
靜態(tài)方法

ES5:

ES5Detective.countCases = function(count) {
  if(!count) {
    console.log("no case solved");
  } else {
    console.log(`${count} cases are solved`);
  }
};

類名后直接定義方法,這個方法就是靜態(tài)方法。

ES5Detective.countCases();

ES6:

class ES6Detective {
  static countCases() {
    console.log(`Counting cases...`);
  }
}

// call it
ES6Detective.countCases();
繼承

ES6使用extends關(guān)鍵字實現(xiàn)繼承。

ES5:

function ES5Detective() {
  var available: boolean = true; // private field.

  this.dectiveName = "Detective who";
  console.log("##ES5Detective contructor");

  this.investigate = function(scene) {
    // 略 
  }

  this.assistant = "assistant who";
}

ES5Detective.prototype.solveCase = function(caseName) {
  // 略
}

// inheritance
function ES5DetectiveConan() {
  // first line in constructor method is a must!!!
  ES5Detective.call(this);

  this.dectiveName = "Conan";
}

// inheritance
ES5DetectiveConan.prototype = Object.create(ES5Detective.prototype);
ES5DetectiveConan.prototype.constructor = ES5DetectiveConan;

ES5繼承的時候需要注意兩個地方:

需要在子類的構(gòu)造函數(shù)里調(diào)用SuperClass.call(this[, arg1, arg2, ...])

子類的prototype賦值為:SubClass.prototype = Object.create(SuperClass.prototype),然后把構(gòu)造函數(shù)重新指向自己的:SubClass.prototpye.constructor = SubClass。

ES6:

class ES6Detective {
  constructor() {
    console.log("Detective constructor");
    this.detectiveName = "Detective who";
    this._bookName = "who";
  }

  solveCase(caseName) {
    if(!caseName) {
      console.log("no case to solve");
    } else {
      console.log("case " + caseName + " is solved");
    }
  }

  get fromBookName() {
    return this._bookName;
  }

  set fromBookName(value) {
    this._bookName = value;
  }

  get bookAuthor() {
    return "Author Who";
  }

  static countCases() {
    console.log(`Counting cases...`);
  }
}

class ES6DetectiveConan extends ES6Detective {
  constructor() {
    super();
    console.log("ES6DetectiveConan constructor");
  }
}

ES6的新語法更加易懂。

注意:一定要在子類的構(gòu)造方法里調(diào)用super()方法。否則報錯。

調(diào)用super類內(nèi)容
class ES6DetectiveConan extends ES6Detective {
  constructor() {
    super();
    console.log("ES6DetectiveConan constructor");
  }

  solveCase(caseName) {
    super.solveCase(caseName);

    if(!caseName) {
      console.log("CONAN no case to solve");
    } else {
      console.log("CONAN case " + caseName + " is solved");
    }
  }
}
靜態(tài)方法可以被繼承

ES6的靜態(tài)方法可以被繼承。ES5的不可以。

class ES6Detective {
  static countCases(place) {
    let p = !place ? "[maybe]" : place;
    console.log(`Counting cases...solve in ${p}`);
  }
}

class ES6DetectiveConan extends ES6Detective {
  constructor() {
    super();
    console.log("ES6DetectiveConan constructor");
  }
}

// static method
ES6Detective.countCases();
ES6DetectiveConan.countCases("Japan");

// result
Counting cases...solve in [maybe]
Counting cases...solve in Japan

在子類ES6DetectiveConan并沒有定義任何方法,包括靜態(tài)方法。但是,在父類和子類里都可以調(diào)用該方法。

甚至,可以在子類里調(diào)用父類的靜態(tài)方法:

class ES6DetectiveConan extends ES6Detective {
  static countCases(place) {
    let p = !place ? "[maybe]" : place;
    super.countCases(p);
    console.log(`#Sub class:- Counting cases...solve in ${p}`);
  }
}

// result
Counting cases...solve in [maybe]
Counting cases...solve in Japan
#Sub class:- Counting cases...solve in Japan
代碼

https://github.com/future-cha...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/80669.html

相關(guān)文章

  • React Native填坑之旅--HTTP請求篇

    摘要:填坑之旅篇填坑之旅動畫填坑之旅請求篇如果不能從頭到尾的建立一個應(yīng)用,那么將失色不少。隨著,內(nèi)置的支持了這個填補(bǔ)回調(diào)地獄大坑的功能。很好的利用了這一點,它的請求返回結(jié)果就是。在界面上顯示異常用,顯示警告使用。 React Native填坑之旅--Button篇React Native填坑之旅--動畫React Native填坑之旅--HTTP請求篇 如果不能從頭到尾的建立一個RN應(yīng)用,那...

    yexiaobai 評論0 收藏0
  • 寫給想做前端的你

    摘要:不過細(xì)想想,我郵只有前端的選修課啥的,課程也不是那么就業(yè)導(dǎo)向。至少目前,很少有大公司完全把作為前后端通用的技術(shù)棧。不能把簡單看做是在服務(wù)端的延展。編譯這個思想在前端領(lǐng)域很重要不改變現(xiàn)有的語言環(huán)境同時進(jìn)行最佳的工程實踐。 P.S. 噴神請繞道,大神勿噴,不引戰(zhàn),不攻擊,不鉆牛角尖。 大二時第一次接觸前端。許多同學(xué)估計都想過要做一個網(wǎng)站,大部分又是從PHP開始的(誰讓它是世界上最好的語言呢...

    JerryWangSAP 評論0 收藏0
  • PostCSS自學(xué)筆記(二)【外篇二】

    摘要:之前有研究過做過假設(shè),在插件列表中,的插件執(zhí)行順序自上而下,一切看起來似乎是沒有任何問題的。再有摘自深入設(shè)計摘自寫的姿勢這兩張圖則應(yīng)該是說明了我之前的假設(shè),插件中的執(zhí)行順序自上而下。先來看看一片來自的這段會不會跟這個有關(guān)呢,我先埋個伏筆。 圖解PostCSS的插件執(zhí)行順序 文章其實是一系列的早就寫完了. 才發(fā)現(xiàn)忘了發(fā)在SegmentFault上面, 最早發(fā)布于https://gitee...

    FleyX 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<