ES6/ES2015 핵심 콘텐츠 노트
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"]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.