成人免费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

2016-08-12 13:55:06

2023-06-24 17:09:06

React前端

2024-07-08 00:00:07

2015-09-22 09:50:36

FacebookAndroid

2017-09-11 14:35:34

編輯器開發環境React

2016-08-12 08:49:46

React NativFacebookNative

2023-06-12 07:00:40

Rust進度任務

2021-03-31 08:33:17

SysTick定時器SysTick定時器

2016-08-15 13:34:37

React NativiOSjs入口

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開發

2017-01-04 10:18:00

React NativScrollViewAndroid

2017-01-11 18:44:43

React Nativ觸摸事件Android
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品欧美一区二区中文字幕视频 | 九色 在线 | 亚洲视频免费在线观看 | 中文一区二区视频 | av网站免费观看 | 日本三级视频 | 九九视频在线观看视频6 | 久久一级 | 日本一区二区三区免费观看 | 久国久产久精永久网页 | 91精品国产91综合久久蜜臀 | 国产成人免费视频 | 久久最新 | 日韩精品在线观看免费 | 91福利在线导航 | 国产一区二区三区亚洲 | 91国内外精品自在线播放 | 在线91| 狠狠婷婷综合久久久久久妖精 | 久久久久亚洲 | 免费人成在线观看网站 | 日本 欧美 三级 高清 视频 | 成人欧美一区二区三区在线播放 | 亚洲精品小视频在线观看 | 日本三级网| 蜜臀av日日欢夜夜爽一区 | 99国内精品久久久久久久 | 久久乐国产精品 | 亚洲一区二区中文字幕 | 国产精品美女久久久久久免费 | 亚洲91精品 | 91在线网 | 久久精品国产亚洲 | 一区二区免费在线观看 | 日韩成人在线免费视频 | 亚洲欧美视频 | 国产精品成人一区二区三区 | 综合色久| 亚洲国产高清在线观看 | 成人国产精品久久久 | h视频网站在线观看 |