es6 笔记

严格模式

"use strict"

类与继承

class Test{
    constructor(str){
        this.str=str;
    }
    GetStr(){
        console.log(this.str)
    }
}
class Test2 extends Test {
    constructor(str) {
        super(str)
    }
    
}

双向绑定核心

var obj = new Object();
Object.defineProperty(obj, 'name', {
    configurable: false,//能否删除此属性,能否修改属性的特性
    writable: true,//是否可以写入
    enumerable: true,//Key值是否可以枚举
    value: '默认值'//
})
//=====================================
var obj = new Object();
Object.defineProperties(obj, {
    name: {
        value: '名',
        configurable: false,
        writable: true,
        enumerable: true
    },
    age: {
        value: 18,
        configurable: true
    }
})

运算符扩展

var arr1 = ['a', 'b'];  
var arr2 = ['c'];  
var arr3 = ['d', 'e'];
[...arr1, ...arr2, ...arr3]//合并数组
//解构
const [first, ...rest] = [1, 2, 3, 4, 5];  
first // 1  
rest // [2, 3, 4, 5]
//拆解数组
[...'hello']  
// [ "h", "e", "l", "l", "o" ]

SET类型

var set = new Set([1, 2, 3, 4, 4]);
[...set]; // [1, 2, 3, 4]//去重
  • 属性:

    • Set.prototype.constructor:构造函数,默认就是Set函数
    • Set.prototype.size:返回实例的成员总数
  • 操作方法:

    • add(value):添加一个值,返回Set结构本身
    • delete(value):删除某个值,返回布尔值
    • has(value):返回布尔值,表示是否是成员
    • clear():清除所有成员,无返回值

展开

let list=[25,50,75,100]
Math.max.apply(Math,list);//100
Math.max.call(Math,25,50,75,100);//100
//=====================
Math.max(...list);

symbol永不重名

symbol

var mySymbol = Symbol();

// 第一种写法
var a = {};
a[mySymbol] = 'Hello!';

// 第二种写法
var a = {
  [mySymbol]: 'Hello!'
};

var s1 = Symbol.for('foo');
var s2 = Symbol.for('foo');

s1 === s2 // true

var s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"

var s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined

去除强偶合

const shapeType = {
  triangle: Symbol()
};

function getArea(shape, options) {
  let area = 0;
  switch (shape) {
    case shapeType.triangle:
      area = .5 * options.width * options.height;
      break;
  }
  return area;
}

getArea(shapeType.triangle, { width: 100, height: 100 });

Object.getOwnPropertySymbols//Symbol遍历方法

const obj = {};
let a = Symbol('a');
let b = Symbol('b');

obj[a] = 'Hello';
obj[b] = 'World';

const objectSymbols = Object.getOwnPropertySymbols(obj);

objectSymbols
// [Symbol(a), Symbol(b)]

New时创建

class Foo {  
    constructor() {  
        console.log( "Foo: ", new.target.name );  
    }  
}  
  
class Bar extends Foo {  
    constructor() {  
        super();  
        console.log( "Bar: ", new.target.name );  
     }  
  
    baz() {  
        console.log( "baz: ", new.target );  
    }  
}  
  
var a = new Foo();  
// Foo: Foo  
  
var b = new Bar();  
// Foo: Bar  
// Bar: Bar  
  
b.baz();  
// baz: undefined  

箭头函数最简写

let ref=val=>val;
let ref=function(val){
    return val
}

跨域方案
Cors
websocket
Jsonp
proxytable
access-control-allow-origin:*

你可能感兴趣的:(es6 笔记)