成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

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

TwIStOy / 3050人閱讀

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

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

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

定義偵探類(lèi)作為例子。

ES5的“類(lèi)”是如何定義的。

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

ES6定義類(lèi):

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

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

屬性

看看這個(gè)偵探是從哪本書(shū)出來(lái)的。

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沒(méi)有setter而賦值的話(huà)就會(huì)出現(xiàn)下面的錯(cuò)誤:

detective.bookAuthor = "A C";
                     ^

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

偵探是如何解決案件的。

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");
  // 實(shí)例方法
  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添加方法非常簡(jiǎn)單直接。ES5中添加實(shí)例方法有兩種方法,一是在prototype里定義,一是在構(gòu)造函數(shù)重定義。在構(gòu)造函數(shù)中定義的實(shí)例方法和屬性在每一個(gè)實(shí)例中都會(huì)保留一份,而在原型中定義的實(shí)例方法和屬性是全部實(shí)例只有一份

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

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`);
    }
  }
}

在其他的方法訪(fǎng)問(wèn)的時(shí)候就會(huì)報(bào)錯(cuò)。

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

ES5:

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

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

ES5Detective.countCases();

ES6:

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

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

ES6使用extends關(guān)鍵字實(shí)現(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繼承的時(shí)候需要注意兩個(gè)地方:

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

子類(lèi)的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的新語(yǔ)法更加易懂。

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

調(diào)用super類(lèi)內(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

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

甚至,可以在子類(lèi)里調(diào)用父類(lèi)的靜態(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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/80669.html

相關(guān)文章

  • React Native填坑之旅--HTTP請(qǐng)求篇

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

    yexiaobai 評(píng)論0 收藏0
  • 寫(xiě)給想做前端的你

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

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

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

    FleyX 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<