Syntax
ECMA μ°ν Task Group μ€μ νλμ ECMA-262 (TC39) μμ μ μ§ λ° κ°μ κ΄λ¦¬ λλ λΆλΆμ΄λ€.
ECMA Script μ νΈν λ° νμ₯λλ λΌμ΄λΈλ¬λ¦¬ νμ€μ κ°λ°μ΄λ€.
ISO λ° IEC JTC λ±μ νμ€μ κ²μνλ€.
ECMAScript
6λ²μ§Έ νμ€ μλμ
μΌλ‘ λ§μ λΆλΆμ΄ κ°μ λ° μΆκ° λμλ€.
Function
κΈ°μ‘΄ ν¨μ μ μΈμ (ES5) μμ λ³κ²½λ μ
Example #1 : ES5
function foo() {
/* statement */
}
Example #2 : ES6
var obj = {
foo() {
/* statement */
},
poo: () => {
/* statement */
}
}
obj.foo
κ³Ό obj.poo
λ λμΌν κΈ°λ₯μ νλ€.
Arrows
νμ΄ν ν¨μ ννμμ μΆμ½ν ν¨μ μ λλ€.
Syntax
(param1, param2, ... , paramN) => { statements }
Parameters
- statements
- ν¨μμ λ΄μ©
Example #1 : ES6
// 맀κ°λ³μκ° νλ μ΄μμλ κ²½μ°λ κ΄νΈκ° νμ
(param1, param2) => {
return param1 + param2
};
// 맀κ°λ³μκ° νλμΈ κ²½μ°μλ κ΄νΈκ° νμ μμ
param1 => {
return param1 + 1
}
// 맀κ°λ³μκ° μμκ²½μ°
() => {
console.log('Hello World');
}
Example #2 : ES6
var list = [2, 4, 6, 8];
// Expression Block μ¬μ© μν¨ (ννμμ κ²°κ³Όκ° λ°νλ¨)
var odd = list.map(value => value + 1); // [3, 5, 7, 9]
// Expression Block μ¬μ© (λΈλ λ΄λΆλ§ μ€ν, λ°νκ°μ μν΄μλ return ννμμ λͺ
μν΄μΌ ν¨)
list.forEach(v => {
console.log(v);
});
Example #3 : ES6
// κ°μ²΄ 리ν°λ΄ λ°ν
var func = () => ({ foo: 1 });
console.log(func()); // {foo: 1}
Expression
- μ μΈ μ»¨ν
μ€νΈμ λ°μ
this
κ°μ κ°μ§λ€.this
κ° λ°μΈλ© λμ§ μλλ€.
function foo() {
this.value = 0;
setInterval(() => {
console.log(this.value); // 0
}, 1000);
}
- μμ±μ (constructor) λ‘ μ¬μ©λ μ μλ€. (new μ°μ°μ μ¬μ© λΆκ°)
var Foo = () => {};
var foo = new Foo(); // TypeError : Foo is not a constructor
Default Parameter
Description
ν¨μμ 맀κ°λ³μμ κΈ°λ³Έκ°μ΄ μ€μ κ°λ₯ν©λλ€.
Syntax
function func_name([param1[= defaultValue1][, ... , paramN[= defaultValueN]]]) {
statements
}
Parameters
- func_name
- ν¨μλͺ
- param1, param2
- νλΌλ©ν° λͺ
- defaultValue1, defaultValue2
- νλΌλ©ν° κΈ°λ³Έκ°
- statements
- ν¨μμ λ΄μ©
Example #1 : ES6
function add(a, b = 1) {
return a * b;
}
console.log(add(5)) // 5
Expression
- ν¨μμλ μ μ© κ°λ₯νλ€.
function callLog(msg = defaultMsg()) {
return console.log(mes);
}
function defaultMsg() {
return 'TEST_LOG';
}
callLog() // TEST_LOG
undefined
μ λ¬ μ
function callLog(msg = 'TEST_LOG') {
return console.log(mes);
}
callLog(undefined) // TEST_LOG
Spread Operator
2κ° μ΄μμ μΈμλ₯Ό λμ΄νλ λ°©μ
Syntax
// ν¨μ νΈμΆμ©
myFunction(...args);
// λ°°μ΄ λ¦¬ν°λ΄μ©
[...args, 5, 6, 7];
// λΉ κ΅¬μ‘°νμ©
[a, b, ...args] = [1, 2, 3, 4, 5];
Example
var org = [3, 4];
var custom = [1, 2, ...org, 5]; // [1, 2, 3, 4, 5]
Expression
- μ€νλ λ μ°μ°μλ‘ λ°°μ΄ & κ°μ²΄ 볡μ .
var array = [1, 2, 3];
var cloneArray = [...array];
console.log(cloneArray); // [1, 2, 3]
var obj = {a: 10, b: 20};
var cloneObj = {...obj};
console.log(cloneObj); // {a: 10, b: 20}
Rest Parameter
λλ¨Έμ§ λ§€κ°λ³μ (rest parameter) ꡬ문μ λλ¨Έμ§ μΈμλ€λ μ μν΄ λνλ λλ€.
Syntax
function (a, b, ...theArgs) {
// statements
}
Example
function foo(...Args) {
console.log(Args.length);
}
foo(); // 0
foo(5); // 2
foo(5, 7, 9); // 3
function foo(arg1, ...Args) {
return Args.map(function (elem) {
return arg1 * elem
});
}
console.log(foo(2, 1, 2, 3)); // [2, 4, 6]
Expression
arguments
κ°μ²΄κ° μλArray
κ°μ²΄μmethod
μ¬μ©μ΄ κ°λ₯νλ€.
function sortRestArgs(...Args) {
return Args.sort();
}
function sortArguments() {
return arguments.sort();
}
console.log(sortRestArgs(5, 3, 7, 1)); // [1, 3, 5, 7]
console.log(sortArguments(5, 3, 7, 1)); // TypeError
Destructuring Syntax
ꡬ쑰λΆν΄ λ¬Έλ²μ ES6 μμ μ¬μ©λλ λ³μμ μ μΈ λ°©μμ λλ€.
Expression
- νΉμ κ°μ²΄μ κ°μ κΊΌλ΄μ€λ λ°©λ²
var josh = {
language: 'javascript',
position: 'front-end',
area: 'pangyo',
hobby: 'singing',
age: '102'
};
var language = josh.language;
var position = josh.position;
var area = josh.area;
var hobby = josh.hobby;
var age = josh.age;
var josh = {
language: 'javascript',
position: 'front-end',
area: 'pangyo',
hobby: 'singing',
age: '102'
};
var { language, position, area, hobby, age } = josh;
console.log(language); // javascript
console.log(position); // front-end
console.log(area); // pangyo
console.log(hobby); // singing
console.log(age); // 102
- ν¨μμ μ μ©
var context = {
commit: actionName => console.log(actionName + ' has been committed!!')
};
var {commit} = context;
commit('addProducts'); // addProducts has been committed !!
Export Syntax
μ§μ λ νμΌμμ κ°μ²΄ (ν¨μ, μ€λΈμ νΈ, μμνμ
) μ export
νλλ° μ¬μ©λλ€.
Syntax
// Named Exports
export { myFunction }; // ν¨μ exports
export const foo = Math.sqrt(2); // μμ exports
// Default Exports (μ€ν¬λ¦½νΈμμ μ μΌνκ² νλ² μ¬μ©)
export default myFunctionOrClass;
μ°Έκ³ μλ£
β Promise Iterable & Iterator β