ES6基礎

let

//let不存在變量提升
console.log(name);
let name = "xiaoming";
//Uncaught ReferenceError: name is not defined

箭頭函數和this

var bob = {
    _name: "bob",
    _friends: ["a", "b"],
    printFriends(){
        this._friends.forEach(f =>{
            console.log(`${this._name} knows ${f}`)
        })
    }
}

function s(){
    let f = () => {
        let numbers = [];
        for(let n of arguments){
            numbers.push(n*n);
        }
        return numbers;
    }
    return f();
}

模板字符串

console.log(`this will be printed in a line`)

console.log(`this will be printed
 in multiline`)

var name = "xiaoming", age = 10;
console.log(`${name} is ${age} years old`);

//不轉義轉義符輸出
console.log(String.raw`In ES5 "\n" is a line-feed.`)

解構

//當一個數組成員嚴格等於undefined,默認值纔會生效
//只要某種數據結構具有 Iterator 接口,都可以採用數組形式的解構賦值
let [,, third, last] = ["a", "b", "c"];
//third "c"
//last undefined

let [head, ...all] = [1, 2, 4, 5];
//head 1
//all [2,4,5]

let [x = 1] = [undefined];
//x 1

let [y = 1] = [null];
//y null

//交換變量的值
var num1 = 1, num2 = 2;
[num1, num2] = [num2, num1];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章