ES6/ES2015 핵심 콘텐츠 노트

2353 단어
Var(글로벌 변수, 함수 변수)
var a = [];
for (let i = 0; i < 10; i++) {
  a[i] = function () {
    console.log(i);
  };
}
a[6](); // 6

Let(블록급 변수, 계수에 사용되는 순환 변수가 누설되지 않도록 함) const monent = require('moment') Const(블록급 상수, 값은 변경할 수 없으며 제3자 라이브러리를 참조할 때 명시된 변수)
class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        console.log(this.type + ' says ' + say)
    }
}

let animal = new Animal()
animal.says('hello') //animal says hello

class Cat extends Animal {
    constructor(){
        super()
        this.type = 'cat'
    }
}

let cat = new Cat()
cat.says('hello') //cat says hello

Class: 클래스 constructor에서 정의한 방법과 속성은 실례적인 대상 자체의 것이고, constructor에서 정의한 방법과 속성은 모든 실력 대상이 공유할 수 있는 extends 키워드로 슈퍼를 계승한다. 부모류의this 대상을 가리킨다.하위 클래스는 constructor 방법에서 슈퍼 방법을 호출해야 합니다. 그렇지 않으면 새 실례를 만들 때 오류가 발생합니다.자류는 자신의this대상이 아니라 부류의this대상을 계승하여 가공하기 때문이다
function(i){ return i + 1; } //ES5
(i) => i + 1 //ES6
function(x, y) { 
    x++;
    y--;
    return x + y;
}
(x, y) => {x++; y--; return x+y}

this의 지향 문제 해결하기;
class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        setTimeout( () => {
            console.log(this.type + ' says ' + say)
        }, 1000)
    }
}
 var animal = new Animal()
 animal.says('hi')  //animal says hi

우리가 화살표 함수를 사용할 때, 함수 체내의this 대상은 정의할 때 있는 대상이지, 사용할 때 있는 대상이 아니다.
$("#result").append(`
  There are ${basket.count} items
   in your basket, ${basket.onSale}
  are on sale!
`);

시작을 반인용부호 (`) 로 표시하고, 변수를 인용합니다.
구성 해제:
let cat = 'ken'
let dog = 'lili'
let zoo = {cat, dog}
console.log(zoo)  //Object {cat: "ken", dog: "lili"}
let dog = {type: 'animal', many: 2}
let { type, many} = dog
console.log(type, many)   //animal 2

default, rest
function animal(type = 'cat'){
    console.log(type)
}
animal()
function animals(...types){
    console.log(types)
}
animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]

좋은 웹페이지 즐겨찾기