Ajax
의 관심도가 증가하고, Server Side
에서 Client Side
로 변화하면서, 현재의 자바스크립트의 고도화 필요성이 생김.
ES6 는 무엇인가
현재 우리가 사용하고 있는 것은 ES5
이고, 2009년에 제정됨. 이후 2015년 ES6 (ECMASCRIPT 2015)
제정되어 현재는 컴파일 과정을 거쳐서 ES5
변환 후 브라우저에서 인식하는 것이 현대의 흐름이다.
컴파일을 위한 TASK RUNNER
그 외에 브라우저가 채택하고 있는 표준
- BOM (Browser Object Model)
- DOM (Document Object Model)
주요 변경 사항
let, const
let.
- 블록단위에 scope (수정 가능)
- 루프문 안에서는 매번 새로 생성됨 (for, while등)
- 윈도우의 속성이 아님
const.
Arrow function
- 함수를 간결하게 작성할 수 있도록 람다식표현중
Arrow
가 추가됨.
| |
| |
| var selected = allJobs.filter(function (job) { |
| return job.isSelected(); |
| }); |
| |
| |
| var selected = allJobs.filter(job => job.isSelected()); |
| |
| |
| var total = values.reduce(function (a, b) { |
| return a + b; |
| }, 0); |
| |
| |
| var total = values.reduce((a, b) => a + b, 0); |
화살표 함수표현식 으로 인해, 이전에 사용하던 this
를 참조하기 위한 구문이 필요치 않다.
| |
| { |
| ... |
| addAll: function addAll(pieces) { |
| var self = this; |
| _.each(pieces, function (piece) { |
| self.add(piece); |
| }); |
| }, |
| ... |
| } |
| |
| |
| { |
| ... |
| addAll: function addAll(pieces) { |
| _.each(pieces, piece => this.add(piece)); |
| }, |
| ... |
| } |
| |
| |
| { |
| ... |
| addAll(pieces) { |
| _.each(pieces, piece => this.add(piece)); |
| }, |
| ... |
| } |
function arguments
| |
| 'use strict'; |
| |
| function loop(func, count = 3) { |
| for (var i = 0; i < count; i++) { |
| func(); |
| } |
| } |
| |
| function sum(...numbers) { |
| return numbers.reduce(function(a, b) { return a + b; }); |
| } |
| |
| loop(function(){ console.log('hello')}); |
| console.log(sum(1, 2, 3, 4)); |
for/for in/for of
| |
| for (var index = 0; index < myArray.length; index++) { |
| console.log(myArray[index]); |
| } |
| |
| |
| myArray.forEach(function (value) { |
| console.log(value); |
| }); |
| |
| |
| for (var index in myArray) { |
| console.log(myArray[index]); |
| } |
For-in 은 객체의 순회를 위한 반복문으로 몇가지 특징을 가진다.
- prototype chain도 순회한다.
- 순서가 무작위이다.
var index
가 string
이다.
For-in도 순회문 이지만 몇가지 특징이 있다.
- break/continue/return 등으로 탈출 가능
- 배열순회용 이기 때문에, 정확히 객체의 배열만 순회한다.
Template String
새로운 문자표현식이 추가되었다.
| console.log(`Hello ${a} I'm ${b} year's old`) |
Module
기능별로 파일을 구분하여 페이지에서 로딩하였으나, export
와 import
구문을 통해 모듈을 로딩 할 수 있도록 추가 되었다.
| |
| |
| |
| export const random = Math.random(); |
| export function double(x) { return 2 * x; } |
| |
| |
| |
| const random = Math.random(); |
| function double(x) { return 2 * x; } |
| |
| export { random, double }; |
다른 모듈 에서는 export
로 불러올 수 있음.
| |
| |
| import { random, double } from './num.js'; |
| |
| |
| import * as num from './num.js'; |
| |
| |
| import { random as ran, double as db } from './num.js'; |
Promise
- 비동기 통신이 늘어나면서 콜백에 대한 번거로움을 해소하고자 도입됨. 통신에 한정되지 않고, 무한하게 활용할 수 있음.
| |
| 'use strict'; |
| |
| function sleep(callback, msec) { |
| setTimeout(callback, msec); |
| } |
| |
| sleep(function(){ |
| console.log('wake!') |
| }, 1000); |
| |
| |
| |
| 'use strict'; |
| |
| function sleep(msec) { |
| return new Promise(function(resolve, reject){ |
| setTimeout(resolve, msec); |
| }); |
| } |
| |
| sleep(1000).then(function(){ |
| console.log('wake!'); |
| }); |
Refer : http://azu.github.io/promises-book/
Class
- 자바스크립트 에서도 클래스를 지원하게 되었다.
| |
| |
| function Box(length, width) { |
| this.length = length; |
| this.width = width; |
| } |
| |
| Box.prototype.calculateArea = function() { |
| return this.length * this.width; |
| } |
| |
| var box = new Box(2, 2); |
| box.calculateArea(); |
| |
| |
| class Box { |
| constructor(length, width) { |
| this.length = length; |
| this.width = width; |
| } |
| |
| calculateArea() { |
| return this.length * this.width; |
| } |
| } |
| |
| let box = new Box(2, 2); |
| box.calculateArea(); |
| |
| class Shape { |
| |
| constructor(color) { |
| this._color = color; |
| } |
| |
| } |
| |
| class Circle extends Shape { |
| |
| constructor(color, radius) { |
| super(color); |
| this.radius = radius; |
| } |
| |
| } |
| |
| 'use strict'; |
| |
| function User(name) { |
| this._name = name; |
| } |
| |
| User.prototype = Object.create(null, { |
| constructor: { |
| value: User |
| }, |
| |
| say: { |
| value: function() { |
| return 'My name is ' + this._name; |
| } |
| } |
| }); |
| |
| function Admin(name) { |
| User.apply(this, arguments); |
| } |
| |
| Admin.prototype = Object.create(User.prototype, { |
| constructor: { |
| value: Admin |
| }, |
| |
| say: { |
| value: function() { |
| var superClassPrototype = Object.getPrototypeOf(this.constructor.prototype); |
| return '[Administrator] ' + superClassPrototype.say.call(this); |
| } |
| } |
| }); |
| |
| var user = new User('Alice'); |
| console.log(user.say()); |
| |
| var admin = new Admin('Bob'); |
| console.log(admin.say()); |
| |
| 'use strict'; |
| |
| class User { |
| constructor(name) { |
| this._name = name; |
| } |
| |
| say() { |
| return 'My name is ' + this._name; |
| } |
| } |
| |
| class Admin extends User { |
| say() { |
| return '[Administrator] ' + super.say(); |
| } |
| } |
| |
| var user = new User('Alice'); |
| console.log(user.say()); |
| |
| var admin = new Admin('Bob'); |
| console.log(admin.say()); |
Generator
await/async