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 的下一个版本,它有很...
    奋斗的小废鱼阅读 4,073评论 0 16
  • 一、ES6简介 ​ 历时将近6年的时间来制定的新 ECMAScript 标准 ECMAScript 6(亦称 ...
    一岁一枯荣_阅读 11,269评论 8 25
  • 你可能已经听说过ECMAScript 6(简称 ES6)了。ES6 是 Javascript 的下一个版本,它有很...
    米塔塔阅读 4,474评论 0 10
  • 三,字符串扩展 3.1 Unicode表示法 ES6 做出了改进,只要将码点放入大括号,就能正确解读该字符。有了这...
    eastbaby阅读 5,449评论 0 8
  • 官方中文版原文链接 感谢社区中各位的大力支持,译者再次奉上一点点福利:阿里云产品券,享受所有官网优惠,并抽取幸运大...
    HetfieldJoe阅读 8,095评论 3 37