ES6部分特性学习

/**
 * test es6
 * @authors Your Name (you@example.org)
 * @date    2017-12-15 19:29:39
 * @version $Id$
 */

//由于学习vue-router需要,学学ES6;在babel中文网看到try it out翻译为试用,第一反应是这货难道是收费的?!
//我还是看英文版的吧
//go~ECMAScript2015 Features


//箭头(函数)和this
//lexical 词汇性的 

var odds = i => {i++ 
    return i}
//输出2 箭头=>左边是参数,右边是函数体,当有多个参数时,用括号括起来
console.log(odds(1))

var boo = i => {
    let str = "boo";
    let foo = j => {
        let str = "foo";
        console.log(j);
        console.log(this.str);
    }
    foo(str);
    console.log(this.str);
}
boo();


//类:ES2015类语法是基于原型的面向对象模型的语法糖(所以理解原型很有必要)
//类提供基于原型的继承,super calls,实例化,静态函数以及构造函数
class superType {
    constructor(x, y){
     this.x = x;
     this.y = y;
    }
    func1(){
        console.log(this.x);
    }
}
class subType extends superType{
    constructor(a, b, c){
        super(a,b);
        this.z = c;
    }
    funcSub(){
        super.func1();
    }
    static func2(){
        //
    }
}

//增强的对象字面量:主要是可以设置原型,简化了foo:foo这样的赋值,可以定义方法,以及super calls

var obj = {
    _proto_ = superType.prototype,
    //相当于foo:foo
    foo,
    func3(){
        return super.toString();
    }
}

//模板字符串
//使用符号``
var name = "tom";
var str = `hello ${name} !`

//解构,解构通过支持匹配数组和对象,允许使用模式匹配绑定值,解构是fail-soft(软失败,即不会报错中止执行,个人理解)
//如果没有找到要匹配的值,则赋值为undefined
// list matching
var [a, ,b] = [1,2,3];
a === 1;
b === 3;

// object matching
var { op: a, lhs: { op: b }, rhs: c }
       = getASTNode()

// object matching shorthand
// binds `op`, `lhs` and `rhs` in scope
var {op, lhs, rhs} = getASTNode()

// Can be used in parameter position
function g({name: x}) {
  console.log(x);
}
g({name: 5})

// Fail-soft destructuring
var [a] = [];
a === undefined;

//默认参数Default、不定参数Rest(用在末尾,使用符号...)、扩展运算符Spread(...可以展开数组或对象)
function f(x, y=12) {
  // 如果没有传入y或传入了undefined,y的默认值为12
  return x + y;
}
f(3) == 15
function f(x, ...y) {
  // y是一个数组
  return x * y.length;
}
f(3, "hello", true) == 6
function f(x, y, z) {
  return x + y + z;
}
// 将数组中的每个元素展开为函数参数
f(...[1,2,3]) == 6

//let const 这两个关键字具有块级作用域,const定义常量

//for-of结构

//unicode编码
// 和ES5.1相同
"𠮷".length == 2

// 正则表达式新的u模式
"𠮷".match(/./u)[0].length == 2

// 新的unicode表示法
"\u{20BB7}" == "𠮷" == "\uD842\uDFB7"

// 新的字符串方法
"𠮷".codePointAt(0) == 0x20BB7

// for of迭代码点
for(var c of "𠮷") {
  console.log(c);
}

//模块Modules

//数据结构 Map Set weakMap weakSet
// Sets
var s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;

// Maps
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;

// Weak Maps
var wm = new WeakMap();
wm.set(s, { extra: 42 });
wm.size === undefined

// Weak Sets
var ws = new WeakSet();
// 由于传入的对象没有其他引用,故将不会被set保存
ws.add({ data: 42 });

//Math String Number对象新增一些方法,以及用于对象复制的Object.assign()方法(可以为一个对象添加另一个对象的内容)
Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false

Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2

"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc"

Array.from(document.querySelectorAll("*")) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1,2,3].findIndex(x => x == 2) // 1
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c"

Object.assign(Point, { origin: new Point(0,0) })


//promise
//
//
//先写这么多吧,后续需要更多的知识点的话,再补充。

评论

暂无评论