ReactNative之ES6與ES5區別

解構復制

ES5
var React = require('react-native');
var View = React.View
ES6
var {View} = require('react-native')

導入模塊

ES5
var other = require('./other');
ES6
import other from './other';
導出模塊
ES5
var myCompnent = React.createClass({
   .....
});
module.exports = myCompnent;
ES6
var myCompnent = React.createClass({
    .....
});
exports default myCompnent;

ES 6語法采用export來代替module.exports

let和const

ES5
var a = 1;
ES6
let a = 1
a = 2
const PI = 3.1415
PI = 3 //error

ES 6 語法使用let聲明變量,const聲明只讀變量

函數簡寫

ES5
render:function(){
    return xxx
}

ES6

render(){
    return xxx
}

箭頭函數

ES5
var callback = function(v){

}.bind(this)

ES 5為了使函數與上下文保持一致,需要使用bind(this)

ES6
let callback =() = >{

}

ES 6使用箭頭函數實現上下文自動綁定

字符串插值

ES5
var s1 = "React"
var s2 = s1 + " Native"
ES6
let s1 = "React"
let s2 = "${s1} Native"

Promise 異步

ES5
try{
this.doAsyncOperation(params,this.onSuccessCallBack,this.onErrorCallBack);
}catch(e){
}
ES6
this.doAsyncOperation(param).then((param) => {
}).catch((e)=>{
})
組件的屬性類型和默認屬性
ES5
var Video = React.createClass({
     getDefaultProps: function() { 
          return { 
            autoPlay: false, 
            maxLoops: 10, 
          };
     }, 
    propTypes: {
         autoPlay: React.PropTypes.bool.isRequired,
         maxLoops: React.PropTypes.number.isRequired,
         posterFrameSrc: React.PropTypes.string.isRequired,
         videoSrc: React.PropTypes.string.isRequired, 
     }, 
    render: function() { 
         return ( <View /> );
     },
});
ES6
class Video extends React.Component {
      static defaultProps = { 
            autoPlay: false, 
            maxLoops: 10, 
        }
      static propTypes = { 
            autoPlay: React.PropTypes.bool.isRequired, 
            maxLoops: React.PropTypes.number.isRequired,
            posterFrameSrc: React.PropTypes.string.isRequired,
            videoSrc: React.PropTypes.string.isRequired, 
        }
      render() { 
            return ( 
                <View /> 
             );
        }
}

初始化STATE

ES5
var Video = React.createClass({ 
      getInitialState: function() {
             return { 
                    loopsRemaining: this.props.maxLoops,
               };
       },
})
ES6
class Video extends React.Component { 
        constructor(props){ 
            super(props); 
            this.state = {
                   loopsRemaining: this.props.maxLoops, 
              }; 
        }
}

參考:https://juejin.im/post/599683b8f265da24996015ca

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 你可能已經聽說過ECMAScript 6(簡稱 ES6)了。ES6 是 Javascript 的下一個版本,它有很...
    奮斗的小廢魚閱讀 771評論 0 16
  • 一、ES6簡介 ? 歷時將近6年的時間來制定的新 ECMAScript 標準 ECMAScript 6(亦稱 ...
    一歲一枯榮_閱讀 6,113評論 8 25
  • 你可能已經聽說過ECMAScript 6(簡稱 ES6)了。ES6 是 Javascript 的下一個版本,它有很...
    米塔塔閱讀 954評論 0 10
  • 三,字符串擴展 3.1 Unicode表示法 ES6 做出了改進,只要將碼點放入大括號,就能正確解讀該字符。有了這...
    eastbaby閱讀 1,566評論 0 8
  • 官方中文版原文鏈接 感謝社區中各位的大力支持,譯者再次奉上一點點福利:阿里云產品券,享受所有官網優惠,并抽取幸運大...
    HetfieldJoe閱讀 3,042評論 3 37