ReactNative之ES6與ES5區(qū)別

解構(gòu)復(fù)制

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

導(dǎo)入模塊

ES5
var other = require('./other');
ES6
import other from './other';
導(dǎo)出模塊
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聲明只讀變量

函數(shù)簡寫

ES5
render:function(){
    return xxx
}

ES6

render(){
    return xxx
}

箭頭函數(shù)

ES5
var callback = function(v){

}.bind(this)

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

ES6
let callback =() = >{

}

ES 6使用箭頭函數(shù)實(shí)現(xiàn)上下文自動(dòng)綁定

字符串插值

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)=>{
})
組件的屬性類型和默認(rèn)屬性
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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 你可能已經(jīng)聽說過ECMAScript 6(簡稱 ES6)了。ES6 是 Javascript 的下一個(gè)版本,它有很...
    奮斗的小廢魚閱讀 806評論 0 16
  • 一、ES6簡介 ? 歷時(shí)將近6年的時(shí)間來制定的新 ECMAScript 標(biāo)準(zhǔn) ECMAScript 6(亦稱 ...
    一歲一枯榮_閱讀 6,209評論 8 25
  • 你可能已經(jīng)聽說過ECMAScript 6(簡稱 ES6)了。ES6 是 Javascript 的下一個(gè)版本,它有很...
    米塔塔閱讀 1,022評論 0 10
  • 三,字符串?dāng)U展 3.1 Unicode表示法 ES6 做出了改進(jìn),只要將碼點(diǎn)放入大括號,就能正確解讀該字符。有了這...
    eastbaby閱讀 1,671評論 0 8
  • 官方中文版原文鏈接 感謝社區(qū)中各位的大力支持,譯者再次奉上一點(diǎn)點(diǎn)福利:阿里云產(chǎn)品券,享受所有官網(wǎng)優(yōu)惠,并抽取幸運(yùn)大...
    HetfieldJoe閱讀 3,132評論 3 37

友情鏈接更多精彩內(nèi)容