ES6基础学习笔记

1、let

 --> 声明变量,只有在它所在的代码块有效;

--> 不变量提升;

--> 在相同的作用域内,不可重复声明同一变量。

2、const

--> 声明只读常量 --> 变量指向的那个地址所保存的数据不得改动。

--> 作用域同 let 。

3、变量解构赋值

//...
let [a,b,c] = [1,2,3];
let [foo, [[bar], baz]] = [a, [[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'}; // foo-aaa  bar-bbb
let {foo:baz} = {foo:'aaa', bar:'bbb'}; // baz-aaa  foo-error:foo is not defined

//...
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, 4]

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

//...
[x,y] = [y,x]; // 交换x、y的值

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

function example(){
    return {foo:1, bar:2};
}
let {foo, bar} = example();

function f([a,b,c]){ ... }
f([1,2,3]);

function f({a,b,c}){ ... }
f({c:3,b:2,a:1});

//...
let jsonData = {
    id: 18,
    status: 'OK',
    data: [100, 200]
};
let {id,status,data:number} = jsonData;

function move({a,b} = {a:0, b:0}){
    return [a,b];
}

//...
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');

4、字符串的扩展

for(let a of 'JavaScript'){
    console.log(a);
}

let a=1;
let b=2;
`${a}+${b}=${a+b}`; // 1+2=3

5、新增方法

一些较常用的新增方法:

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、付费专栏及课程。

余额充值