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()
发布了76 篇原创文章 · 获赞 26 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_29326717/article/details/103444191