ES6一点总结

前言
react项目需要有constructor

constructor(props){
    super(props);
  }

或者不要构造器
但不能

constructor(){
  }

否则
ReferenceError: Must call super constructor in derived class before accessing ‘this’ or returning from derived constructor

const,let
1.const,let不存在变量提升,而且有块级作用域,典型例子在于
for循环可以循环输出每一个的值,var不行
2.const是常量,let不是
3.暂时性死区:就是先使用再赋值会报错

var tmp = 123;

if (true) {
  tmp = 'abc'; // ReferenceError
  let tmp;
}

因为if语句是一个块级作用域所以外面定义的tmp与里面无关

class
1.this指向

import React from 'react';

class Animal extends React.Component {
   constructor(props){
    super(props);
    this.type="animal";
    this.state={
      Name:"123"
    };
    
  }
  says(say){
    console.log(this);
    const p=this;
    setTimeout(function(){
    //报错,因为this默认指向类的实例,会找不到这个Name
      console.log(this.state.Name + ' says ' + say);
      //可以通过,因为给this绑定了值
      console.log(p.state.Name + ' says ' + say);
  }, 1000)
  }

  barrow(param){
    setTimeout(()=>{
    //可以通过,箭头函数
      console.log(this.state.Name+'param'+param)
    },1000)
  }

  render() {
    let animal=new Animal();
    animal.says("sb");
    animal.barrow("hi");
    return (
      <div>ES6</div>
    );
  }
}
  

export default Animal;

解构
1.

let cat = 'ken'
    let dog = 'lili'
    let zoo={cat,dog};
    console.log(zoo);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值