我的 ES6 笔记

let → 声明变量,只有在它所在的代码块有效,不变量提升,在相同的作用域内,不可重复声明同一变量。

const → 声明只读常量 → 变量指向的那个地址所保存的数据不得改动,作用域同 let 。

1、变量解构赋值:

let [a,b,c] = [1,2,3];
let [foo, [[bar], baz]] = [1, [[2], 3]];
let [,,third] = ['foo', 'bar', 'baz']; // third 为 'baz'
let [x,,y] = [1,2,3]; // x 为1, y 为3
let [head, ...tail] = [1,2,3,4]; // head 为1, tail 为 2,3,4
let [x,y,...z] = ['a']; // x 为'a',y 为 undefined, z 为 []
let {foo, bar} = {foo: 'aaa', bar: 'bbb'};
let {foo: baz} = {foo: 'aaa', bar: 'bbb'}; // error: foo is not defined,baz 为 'aaa'
const [a,b,c,d,e] = 'hello'; // a 为 'h',b 为 'e',c 为 'l',d 为 'l',e 为 'o'
let { length: len } = 'hello'; // len 为 5
let {toString: s} = 123; // s === Number.prototype.toString ⇒ true
let {toString: s} = true; // s === Boolean.prototype.toString ⇒ true
function add([x, y]){ return x+y; } // add([1, 2]); ⇒ 3
[[1,2], [3,4]].map(([a, b]) ⇒ a+b); // [3, 7]

function move({x=0, y=0} = {}){ return [x, y]; } 
// move({x:3, y:8});⇒ [3, 8] ; move();⇒ [0, 0]; move({});⇒ [0, 0]; move({x=3});⇒ [3, 0]

let jsonData = {id: 42, status: 'OK', data: [867, 5309]};
let {id, status, data: number} = jsonData; // id 为42,status 为'OK',data 为[867, 5309]

function move({x,y} = {x:0,y:0}){ return [x, y]; }

[x,y] = [y,x];

function example(){ retutn [1,2,3]; }
let [a,b,c] = example();
function example(){ return { foo: 1, bar: 2 }; }
let {foo, bar} = example();

function f([x,y,z]){...}
f([1,2,3]);
function f({x,y,z}){...}
f({z:3,y:2,x:1});

const map = new Map();
map.set('first', 'hello');
map.set('second', 'world');
for(let [key, value] of map){
    console.log(key + 'is' + value);
}
const { SourceMapConsumer, SourceNode } require('source-map);

2、字符串的扩展:

for(let codePoint of 'foo'){
    console.log(codePoint);
}
let x=1;let y = 2;
`${x}+${y}=${x+y}`

3、新增方法

includes()、startsWith()、endsWith()

repeat()

padStart()、padEnd()、trimStart()、trimEnd()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值