ES6快速复习

教程地址:https://www.bilibili.com/video/av70550385

let 为声明块作用域的变量

{let xhr='a'} console.log(xhr);//xhr is not defined


 const 为恒量,声明一次后不允许再次声明该变量,但若是声明为数组则可以向数组内加入数据,因为const仅仅是禁止 赋值这个动作并不禁止该恒量做内部改变。

const xhr='a';
const xhr='b';
console.log(xhr);//Identifier 'xhr' has already been declared
const xhr=[];xhr.push('a');console.log(xhr);

解构数组
function breakfast(){
  return ['a','b','c'];
}
var xhr = breakfast(); one=xhr[0];two=xhr[1];three=xhr[2];
console.log(one,two,three);
let [one,two,three] = breakfast();//可以直接这样赋值,let可以改成var也是可以的。这是es6的语法
console.log(one,two,three);

解构对象
function breakfast(){
  return ['a','b','c'];
}
let {one,two,three}= breakfast();//可以直接这样赋值,let可以改成var也是可以的。这是es6的语法
console.log(one,two,three);

模板字符串
let one='a',two='b';
let three = 'ceshi'+one+two;
console.log(three);
let one='a',two='b';
let three = `ceshi ${one} ${two}`;
console.log(three);

判断字符串里是否包含其他字符串
let one=‘abcdef’;
console.log(one.startsWith('a'));//是否以字符串a开头,返回true
console.log(one.endsWith('a'));//是否以字符串a结尾,返回false
console.log(one.includes('c'));//是否包含字符串a,返回true

函数定义默认值
function  ceshi(one='a'){
  return one;//当one没有值时返回字符串a
}

展开操作符
let one = ['a','b']
console.log(...one);//未加...操作符的时候是直接输出数组,如果加了则挨个输出字符串

 剩余操作符
function ceshi(one,two,...three){
  console.log(one,two,three);
  console.log(one,two,...three);//输出a,b,c,d 因为这儿是展开操作符
}
ceshi('a','b','c');//输出字符串a,b和数组three中的内容c
ceshi('a','b','c','d');//输出字符串a,b和数组three中的内容c和d

 //函数的名字
let one = function ceshi(){}
console.log(one.name);

//箭头函数
ceshi => {
 return bianliang;
}

//把对象复制到另一个对方里
let xhr = {};
Object.assign(xhr,{a:'one'});
Object.assign(xhr,{c:'b'});
console.log(xhr);//{a: "one", c: "b"}

//静态方法不需要实例化类就可以直接使用
class ceshi{
  static xhr(){
    console.log('a');
  }
}
ceshi.xhr();

 //继承
class  one{
  yige(){
    console.log('yige');
  }
}
class two extends one{
  erge(){
    console.log('erge');
  }
}
let aa = new two;
aa.yige();//输出字符串yige

 
 
posted @ 2019-05-05 16:21  学画人生  阅读(162)  评论(0编辑  收藏  举报