前言
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);