๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive ๋์์ 26์ฅ์ ์ ๋ฆฌํ์์ต๋๋ค.
ES6 ์ด์ ์ ๋ชจ๋ ํจ์๋ ์ผ๋ฐ ํจ์๋ก์ ํธ์ถํ ์ ์๋ ๊ฒ์ ๋ฌผ๋ก ์์ฑ์ ํจ์๋ก์ ํธ์ถํ ์ ์๋ค. ๋ค์ ๋งํด, ES6 ์ด์ ์ ๋ชจ๋ ํจ์๋ callable์ด๋ฉด์ contructor์ด๋ค.
var foo = function () {}
foo() // undefined
new foo() // foo {}
๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋ ํจ์๋ callable์ด๋ฉด์ contructor์ด๋ค.
ES6 ํจ์์ ๊ตฌ๋ถ | contructor | prototype | super | arguments |
---|---|---|---|---|
์ผ๋ฐ ํจ์(Normal) | O | O | X | O |
๋ฉ์๋(Method) | X | X | O | O |
ํ์ดํ ํจ์(Arrow) | X | X | X | X |
ES6 ์ฌ์์์ ๋ฉ์๋๋ ๋ฉ์๋ ์ถ์ฝ ํํ์ผ๋ก ์ ์๋ ํจ์๋ง์ ์๋ฏธํ๋ค.
const obj = {
x: 1,
// foo๋ ๋ฉ์๋
foo() { return this.x; }
// bar์ ๋ฐ์ธ๋ฉ๋ ํจ์๋ ๋ฉ์๋๊ฐ ์๋ ์ผ๋ฐ ํจ์๋ค.
bar: function() {
return this.x;
}
}
new obj.foo() // obj.foo is not a constructor
new obj.bar() // bar {}
ES6์์ ์ ์ํ ๋ฉ์๋๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์๋ non-constructor๋ค.
๊ทธ๋ฆฌ๊ณ ES6 ๋ฉ์๋๋ ์์ ์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ด๋ถ ์ฌ๋กฏ [[HomeObject]]๋ฅผ ๊ฐ๋๋ค. super ์ฐธ์กฐ๋ ๋ด๋ถ ์ฌ๋กฏ [[HomeObject]]๋ฅผ ์ฌ์ฉํ์ฌ ์ํผํด๋์ค์ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๋ฏ๋ก ๋ด๋ถ ์ฌ๋กฏ [[HomeObject]]๋ฅผ ๊ฐ๋ ES6 ๋ฉ์๋๋ super ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
// 26-09
const base = {
name: 'Lee',
sayHi() {
return `Hi ${this.name}`
}
}
const derived = {
__proto__: base,
// sayHi๋ ES6 ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์ [[HomeObject]]๋ฅผ ์ง๋ค.
// sayHi์ [[HomeObject]]๋ sayHi๊ฐ ๋ฐ์ธ๋ฉ๋ ๊ฐ์ฒด์ธ derived๋ฅผ ๊ฐ๋ฆฌํค๊ณ
// super๋ sayHi์ [[HomeObject]]์ ํ๋กํ ํ์
์ธ base๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
sayHi() {
return `${super.sayHi()}. how are you doing?`
}
}
console.log(derived.sayHi()) // Hi! Lee. how are you doing?
ํ์ดํ ํจ์๋ function ํค์๋ ๋์ ํ์ดํ๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ์กด์ ํจ์ ์ ์ ๋ฐฉ์๋ณด๋ค ๊ฐ๋ตํ๊ฒ ํจ์๋ฅผ ์ ์ํ ์ ์๋ค.
// ํจ์ ์ ์
const multiply = (x, y) => x * y
multiply(2,3) // 6
// ๋งค๊ฐ๋ณ์ ์ ์ธ
const arrow = (x, y) => { ... }
const arrow = x => { ... }
const arrow = () => { ... }
// ํจ์ ๋ชธ์ฒด ์ ์
const power = x => x ** 2
power(2) // 4
const arrow = () => {
const x = 1
}
// ๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐํ
const create = (id, content) => ({ id, content })
// ์ฆ์ ์คํ ํจ์
const person = (name = > ({
sayHi() {
return `Hi~ ${name}`
}
}))('Lee')
// ํ์ดํ ํจ์๋ ์ผ๊ธ ๊ฐ์ฒด์ด๋ฏ๋ก map, filter, reduce์ ์ธ์๋ก ์ ๋ฌ ๊ฐ๋ฅ
[1, 2, 3].map(v => v * 2)
- ํ์ดํ ํจ์๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์๋ non-contructor๋ค.
- ์ค๋ณต๋ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ ์ธํ ์ ์๋ค.
- ํ์ดํ ํจ์๋ ํจ์ ์์ฒด์ this, arguments, super, new.target ๋ฐ์ธ๋ฉ์ ๊ฐ์ง ์๋๋ค.
ํ์ดํ ํจ์๊ฐ ์ผ๋ฐ ํจ์์ ๊ตฌ๋ณ๋๋ ๊ฐ์ฅ ํฐ ํน์ง์ this์ด๋ค. ํ์ดํ ํจ์๋ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ์ ๋ฌ๋์ด ์ฝ๋ฐฑ ํจ์๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
*ํจ์๋ฅผ ํธ์ถํ ๋ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
class Prefixer {
constructor(prefix) {
this.prefix = prefix
}
add(arr) {
// ์ด ์ฅ์์์ this๋ prefix๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
return arr.map(function (item) {
// ์ฌ๊ธฐ์ this๋ undefined๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
return this.prefix + item
// TypeError: Cannot read property 'prefix' of undefined
})
}
}
const prefixer = new Prefixer('-webkit-')
console.log(prefixer.add(['transition','user-select']))
์์ ๋ฌธ์ ๋ฅผ ES6์์๋ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ "์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this ๋ฌธ์ "๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
ํ์ดํ ํจ์ ๋ด๋ถ์์ this๋ฅผ ์ฐธ์กฐํ๋ฉด ์์ ์ค์ฝํ์ this๋ฅผ ๊ทธ๋๋ก ์ฐธ์กฐํ๋ค. ์ด๋ฅผ lexical this๋ผ ํ๋ค.
ํ์ดํ ํจ์๋ ํจ์ ์์ฒด์ super ๋ฐ์ธ๋ฉ์ ๊ฐ์ง ์๋๋ค. ๋ฐ๋ผ์ ํ์ดํ ํจ์ ๋ด๋ถ์์ super์ ์ฐธ์กฐํ๋ฉด this์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ์ค์ฝํ์ super๋ฅผ ์ฐธ์กฐํ๋ค.
super
ํ์ดํ ํจ์๋ ํจ์ ์์ฒด์ arguments ๋ฐ์ธ๋ฉ์ ๊ฐ์ง ์๋๋ค. super์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ์ค์ฝํ์ arguments๋ฅผ ์ฐธ์กฐํ๋ค.
(function() {
const foo = () => console.log(arguments) // [Arguments] { '0': 1, '1': 2}
๋ใ
(3, 4)
}(1, 2)
const foo = () => console.log(arguments)
foo(1, 2) // ReferenceError: arguments is not defined
arguments ๊ฐ์ฒด๋ ๋ฐฐ์ด์ด ์๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฏ๋ก ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Function.prototype.call์ด๋ Function.prototype.apply ๋ฉ์๋๋ฅผ ์ฌ์ฉํด arguments ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํํด์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์์๋ค.
function sum(...args) {
// args๋ฅผ rest ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด๋ก ์ง์ ๋ฐ์์ ์ฌ์ฉํ๋ฉด ๊ฐ๋ฅํ๋ค.
var array = Array.prototype.slice.call(arguments)
return array.reduce((pre, cur) => pre + cur, 0)
}
์ธ์๊ฐ ์ ๋ฌ๋์ง ์์ ๋งค๊ฐ๋ณ์์ ๊ฐ์ undefined์ด๋ค.
ES6์์ ๋์ ๋ ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ๋ฉด ํจ์ ๋ด์์ ์ํํ๋ ์ธ์ ์ฒดํฌ ๋ฐ ์ด๊ธฐํ๋ฅผ ๊ฐ์ํํ ์ ์๋ค.
function sum(x = 0, y = 0) {
return x + y
}
console.log(sum(1, 2)) // 3
console.log(sum(1)) //1
*rest ํ๋ผ๋ฏธํฐ์๋ ๊ธฐ๋ณธ๊ฐ์ ์ง์ ํ ์ ์๋ค.
Ref
- ์ด์ ๋ชจ ์ , โ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Diveโ, ์ํค๋ถ์ค