ES6 Study Notes

2018-09-07  本文已影响8人  _不能说的秘密i

ES6 Study Notes

在线文档: http://es6.ruanyifeng.com/#README
离线下载: https://github.com/ruanyf/es6tutorial/

变量 Variable

解构赋值 Destructuring

let [a, b, c] = [1, 2, 3];
console.log(a); // 1
let { name, age } = { name: 'test', age: 10} ;
console.log(name); // test
console.log(age);  // 10
let { name, age, hobby:[x, y]} = { name: 'test', age: 10, hobby:['css', 'javascript']};
console.log(x); // css
console.log(y); // javascript

字符串扩展 String

数值扩展 Number

函数扩展 Function

// 简单使用
function defaultArgs(a, b='args') {
    console.log(a, b);
}

defaultArgs('hello'); // hello args
defaultArgs('hello', 'world'); // hello world

// 结合 解构赋值 来使用
function ajax({ method, url, prams, dataType }) {
  console.log(method);
}
function getSum(...values) {
    let sum = 0;
    for (var val of values) {
        sum += val;
    }
    return sum;
}
getSum(1, 2, 3, 4); // 10
// before es6
$('#ele').click(function(i){
    alert(1)
});

// es6
$('#ele').click( i => {
    alert(1);
});
// before es6
let fn1 = function (a, b) {
    return a + b;
}

// es6
let fn2 = (a, b) => a + b;
// before es6
let fn1 = function (a) {
    return parseInt(a);
}

// es6
let fn2 =  a => parseInt(a);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>arrow function  this</title>
</head>
<body>
    <input type="text" id="input">
    <script>
    let input = document.getElementById('input');

    // input.onfocus = function fn1() {
        // console.log(this); // <input type="text" id="input">
    // }

    input.onfocus = () => {
        console.log(this); // window, 自己家没有就往上一层找
    }
</script>
</body>
</html>

数组扩展 Array

console.log(1, ...[2, 3, 4], 5); // 1 2 3 4 5

[...document.querySelectorAll('div')];  // [<div>, <div>, <div>]


function func(x, y, z) {
    return x+y+z;
}
let args = [0, 1, 2];
func(...args);
let arr = [1, 5, 10, 15];
result = arr.find((value, index, arr) => value > 8);
console.log(result); // 10:查找符合条件的第一个值, 没有返回 undefined
let arr = [1, 5, 10, 15];
result = arr.findIndex(function(value, index, arr) => value > 8);
console.log(result); // 2:查找符合条件的第一个值的下标, 没有返回 -1
let arr = new Array(3).fill(1);
console.log(arr); // [1, 1, 1]
let arr = ['tom', 'jack', 'alex', 'jason'];

console.log(arr.keys());

console.log(arr.values());

console.log(arr.entries());
let arr = ['tom', 'jack', 'alex', 'jason'];

let result = arr.includes('tom');
console.log(result); // true

let result2 = arr.includes('mark');
console.log(result2); // false

对象的扩展 Object

let name = "alex";
let age  = 18;
let height = 172;

let obj = {name, age, height}; // 等同于: let obj = {name:name, age:age, height:height};
const user = {
    name: 'alex',
    age : 18,
    height: 172
};

// console.log(Object.keys(user));

for (let i of Object.keys(user)) {
    console.log(i);
}
let obj = {
    name : "alex",
    age  : 18,
    height : 172
};

// objectValues = Object.values(obj);

Object.values(obj).forEach(item => {
    console.log(item);
});

除此之外,ES6还有很多的新语法

以上这些, 有兴趣, 请自行看文档学习吧~~ ^_^

上一篇 下一篇

猜你喜欢

热点阅读