您现在的位置:首页 >> 前端 >> 内容

ES6笔记之一letconst解构

时间:2017/6/19 9:20:00 点击:

  核心提示:let 块级作用域//不存在变量的声明提前console.log(a);//errlet a=0;{let a=10;var b=100;}console.log(a); //ReferenceErr...

let 块级作用域

//不存在变量的声明提前
console.log(a);//err
let a=0;


{
  let a=10;
  var b=100;
}
console.log(a); //ReferenceError: a is not defined
console.log(b); //100
-------
--
//for循环{}内单独子作用域不去()中
for(let i=0;i<5;i++){
let i='aaa';
console.log(i);}
//输出5个aaa
---------
--
var a=a; //succ
let a=a; //err
//let不能重复声明
let a=0;
let a=1;
//以下正确,不同的域内  可以嵌套
{
 let a=0;
 {
  let a=1;
 }
}

const 常量

const PI=3.14159265;
PI=3.14 //err 无法修改常量
//const 不能重复声明

//const 声明时即需赋值否则报错
const AA; //err

解构

Array

let [a,b,c]=[1,2,3];
//以上等价于
let a=1;
let b=2; 
let c=3;

let [x,y]=[1,2,3];
// x=1; y=2;
let [a,[b],d]=[1,[2,3],4];
//a=1,b=2,d=4;


//等号右边如果不是数组(可遍历的结构)报错。 
let [a]=1;
let [a]=false;
let [a]= null;
let [a] ={};
//允许设置默认值
let [x,y='3']=['abc'];
//x='abc',y='3';

Object

let {a,b}={a:'name',b:'age'}
// a=name,b=age;
let {a,b}={b:'age',a:'name'}
//同上,与顺序无关,与属性名对应

String

const [a,b,c,d]='good';
//a=g;b=o;c=0;d=d;

let {length:len}='good';
//len=4;

Number Boolean

let {toString:s}=123;
s===Number.prototype.toString //true;
let {toString:s}=true;
s===Boolean.prototype.toString //true;

函数参数

function add([a,b]){
  return a+b;
}
add([3,2]);  //5

圆括号

//变量声明时不能带 以下均错
let [(a)] = [1]
let {x:{c}}={}
let ({x:c})={}
let {(x:c)}={}
let {(x):c}={}
//函数参数属于变量声明也不能带
function ab([(a)]){return a;}

Tags:ES S6 6笔 笔记 
作者:网络 来源:随风而去