成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 前端
無論React還是RN都已經邁入了ES6的時代,甚至憑借Babel的支持都進入了ES7。ES6內容很多,本文主要講解類相關的內容。

[[173899]]

無論React還是RN都已經邁入了ES6的時代,甚至憑借Babel的支持都進入了ES7。ES6內容很多,本文主要講解類相關的內容。

構造函數

定義偵探類作為例子。

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

  1. function ES5Detective() { 
  2.   console.log('##ES5Detective contructor'); 
  3.  

ES6定義類:

  1. class ES6Detective { 
  2.   constructor() { 
  3.     console.log('Detective constructor'); 
  4.   } 
  5.  

ES6使用了class關鍵字,而且有專門的constructor。ES5里的function ES5Detective既是類的定義,也是構造函數。

屬性

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

ES5:

  1. ES5Detective.prototype.fromBookName = 'who'

ES6:

  1. class ES6Detective { 
  2.   detectiveName: string; 
  3.   _bookName: string; 
  4.  
  5.   constructor() { 
  6.     console.log('Detective constructor'); 
  7.     this.detectiveName = 'Detective who'; // 屬性 
  8.   } 
  9.  

ES6 getter & setter

  1. class ES6Detective { 
  2.   detectiveName: string; 
  3.   _bookName: string; 
  4.  
  5.   constructor() { 
  6.     console.log('Detective constructor'); 
  7.     this.detectiveName = 'Detective who'
  8.     this._bookName = 'who'
  9.   } 
  10.  
  11.   get fromBookName() { 
  12.     return this._bookName; 
  13.   } 
  14.  
  15.   set fromBookName(value) { 
  16.     this._bookName = value; 
  17.   } 
  18.  

如果只有getter沒有setter而賦值的話就會出現下面的錯誤:

  1. detective.bookAuthor = 'A C'
  2.                      ^ 
  3.  
  4. TypeError: Cannot set property bookAuthor of #<ES6Detective> which has only a getter  

實例方法

偵探是如何解決案件的。

ES5:

  1. ES5Detective.prototype.solveCase = function(caseName) { 
  2.   var dn = this.dectiveName; 
  3.   if(!caseName) { 
  4.     console.log('SOLVE CASE: ' + dn + ' no case to solve'); 
  5.   } else { 
  6.     console.log('SOLVE CASE: ' + dn + ' get case ' + caseName + ' is solved'); 
  7.   } 
  8. };  

或者:

  1. function ES5Detective() { 
  2.   this.dectiveName = 'Detective who'
  3.   console.log('##ES5Detective contructor'); 
  4.   // 實例方法 
  5.   this.investigate = function(scene) { 
  6.     console.log('investigate ' + scene); 
  7.   } 
  8.  
  9.   this.assistant = "assistant who"
  10.  

ES6: 

  1. class ES6Detective { 
  2.   detectiveName: string; 
  3.   _bookName: string; 
  4.  
  5.   constructor() { 
  6.     console.log('Detective constructor'); 
  7.     this.detectiveName = 'Detective who'
  8.     this._bookName = 'who'
  9.   } 
  10.  
  11.   solveCase(caseName) { 
  12.     if(!caseName) { 
  13.       console.log('no case to solve'); 
  14.     } else { 
  15.       console.log('case ' + caseName + ' is solved'); 
  16.     } 
  17.   } 
  18.  

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

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

  1. function ES5Detective() { 
  2.   console.log('##ES5Detective contructor'); 
  3.  
  4.   var available: boolean = true; // private field. default income is ZERO. 
  5.   this.investigate = function(scene) { 
  6.     if (available) { 
  7.       console.log('investigate ' + scene); 
  8.     } else { 
  9.       console.log(`i'm not available`); 
  10.     } 
  11.   } 
  12.  

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

  1. if (!available) { 
  2.  
  3.  

靜態方法

ES5:

  1. ES5Detective.countCases = function(count) { 
  2.   if(!count) { 
  3.     console.log('no case solved'); 
  4.   } else { 
  5.     console.log(`${count} cases are solved`); 
  6.   } 
  7. };  

類名后直接定義方法,這個方法就是靜態方法。

  1. ES5Detective.countCases(); 

ES6:

  1. class ES6Detective { 
  2.   static countCases() { 
  3.     console.log(`Counting cases...`); 
  4.   } 
  5.  
  6. // call it 
  7. ES6Detective.countCases();  

繼承

ES6使用extends關鍵字實現繼承。

ES5:

  1. function ES5Detective() { 
  2.   var available: boolean = true; // private field. 
  3.  
  4.   this.dectiveName = 'Detective who'
  5.   console.log('##ES5Detective contructor'); 
  6.  
  7.   this.investigate = function(scene) { 
  8.     // 略  
  9.   } 
  10.  
  11.   this.assistant = "assistant who"
  12.  
  13. ES5Detective.prototype.solveCase = function(caseName) { 
  14.   // 略 
  15.  
  16. // inheritance 
  17. function ES5DetectiveConan() { 
  18.   // first line in constructor method is a must!!! 
  19.   ES5Detective.call(this); 
  20.  
  21.   this.dectiveName = 'Conan'
  22.  
  23. // inheritance 
  24. ES5DetectiveConan.prototype = Object.create(ES5Detective.prototype); 
  25. ES5DetectiveConan.prototype.constructor = ES5DetectiveConan;  

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

  1. 需要在子類的構造函數里調用SuperClass.call(this[, arg1, arg2, ...])
  2. 子類的prototype賦值為:SubClass.prototype = Object.create(SuperClass.prototype),然后把構造函數重新指向自己的:SubClass.prototpye.constructor = SubClass。

ES6:

  1. class ES6Detective { 
  2.   constructor() { 
  3.     console.log('Detective constructor'); 
  4.     this.detectiveName = 'Detective who'
  5.     this._bookName = 'who'
  6.   } 
  7.  
  8.   solveCase(caseName) { 
  9.     if(!caseName) { 
  10.       console.log('no case to solve'); 
  11.     } else { 
  12.       console.log('case ' + caseName + ' is solved'); 
  13.     } 
  14.   } 
  15.  
  16.   get fromBookName() { 
  17.     return this._bookName; 
  18.   } 
  19.  
  20.   set fromBookName(value) { 
  21.     this._bookName = value; 
  22.   } 
  23.  
  24.   get bookAuthor() { 
  25.     return 'Author Who'
  26.   } 
  27.  
  28.   static countCases() { 
  29.     console.log(`Counting cases...`); 
  30.   } 
  31.  
  32. class ES6DetectiveConan extends ES6Detective { 
  33.   constructor() { 
  34.     super(); 
  35.     console.log('ES6DetectiveConan constructor'); 
  36.   } 
  37.  

ES6的新語法更加易懂。

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

調用super類內容

  1. class ES6DetectiveConan extends ES6Detective { 
  2.   constructor() { 
  3.     super(); 
  4.     console.log('ES6DetectiveConan constructor'); 
  5.   } 
  6.  
  7.   solveCase(caseName) { 
  8.     super.solveCase(caseName); 
  9.  
  10.     if(!caseName) { 
  11.       console.log('CONAN no case to solve'); 
  12.     } else { 
  13.       console.log('CONAN case ' + caseName + ' is solved'); 
  14.     } 
  15.   } 
  16.  

靜態方法可以被繼承

ES6的靜態方法可以被繼承。ES5的不可以。

  1. class ES6Detective { 
  2.   static countCases(place) { 
  3.     let p = !place ? '[maybe]' : place; 
  4.     console.log(`Counting cases...solve in ${p}`); 
  5.   } 
  6.  
  7. class ES6DetectiveConan extends ES6Detective { 
  8.   constructor() { 
  9.     super(); 
  10.     console.log('ES6DetectiveConan constructor'); 
  11.   } 
  12.  
  13. // static method 
  14. ES6Detective.countCases(); 
  15. ES6DetectiveConan.countCases('Japan'); 
  16.  
  17. // result 
  18. Counting cases...solve in [maybe] 
  19. Counting cases...solve in Japan  

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

甚至,可以在子類里調用父類的靜態方法:

  1. class ES6DetectiveConan extends ES6Detective { 
  2.   static countCases(place) { 
  3.     let p = !place ? '[maybe]' : place; 
  4.     super.countCases(p); 
  5.     console.log(`#Sub class:- Counting cases...solve in ${p}`); 
  6.   } 
  7.  
  8. // result 
  9. Counting cases...solve in [maybe] 
  10. Counting cases...solve in Japan 
  11. #Sub class:- Counting cases...solve in Japan  

代碼

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

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2024-04-16 08:08:54

DTC國產庫產品

2012-09-11 11:29:25

2016-10-13 19:01:59

React NativUbuntu

2021-11-18 08:55:49

共享CPU內存

2024-06-04 22:20:02

2015-09-22 09:50:36

FacebookAndroid

2016-08-12 08:49:46

React NativFacebookNative

2017-09-11 14:35:34

編輯器開發環境React

2016-08-12 13:55:06

2023-06-24 17:09:06

React前端

2024-07-08 00:00:07

2023-06-12 07:00:40

Rust進度任務

2021-03-31 08:33:17

SysTick定時器SysTick定時器

2017-01-11 18:44:43

React Nativ觸摸事件Android

2017-01-04 10:18:00

React NativScrollViewAndroid

2017-03-09 13:29:04

ReactNative JSPatch

2017-03-21 21:37:06

組件UI測試架構

2024-01-19 09:03:06

ReactTypeScripFlexbox

2024-02-20 01:53:01

ReactFlutter開發

2016-08-15 13:34:37

React NativiOSjs入口
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 操久久 | 一区二区三区不卡视频 | 久久久这里都是精品 | 欧美一级一 | 午夜国产一级片 | 夜夜草导航| 超碰成人在线观看 | 欧美日韩成人在线观看 | 成人午夜免费在线视频 | 国产区一区 | 911精品国产 | 精品久久久久久久久久久 | 日韩精品一区二区三区在线播放 | 国产精品毛片一区二区三区 | 国产精品九九视频 | 国产精品久久久久久久久久久新郎 | 99精品视频在线 | 国产日韩av一区二区 | 北条麻妃av一区二区三区 | 日本一区二区三区在线观看 | 精品欧美一区二区在线观看视频 | 美女日批免费视频 | 国产黄色电影 | 羞羞的视频在线看 | 精品国产91乱码一区二区三区 | 手机av免费在线 | 欧美成人免费在线 | 久久黄色精品视频 | 天堂久久一区 | 视频在线日韩 | 亚洲乱码一区二区三区在线观看 | 久久这里只有精品首页 | 久久国| 成人乱人乱一区二区三区软件 | 国产精品久久久久aaaa九色 | 欧美日韩在线精品 | 国产精品夜夜春夜夜爽久久电影 | 99爱在线免费观看 | 亚洲人免费视频 | 国产精品久久久久久久久久久久 | 蜜臀网 |