ES6 in depth
2017-12-30
κ°μΈμ μΈ κ³΅λΆ λ ΈνΈλ‘, λ΄μ©μ μ€λ₯κ° μμ μ μμ΅λλ€.
ES6μ class
λ μ¬μ€ functionμ΄λ€.(typeof
λ₯Ό μ°μΌλ©΄ "function"
μΌλ‘ λμ¨λ€.) λ¨μ§ prototype κΈ°λ° μμλ³΄λ€ λͺ
ννκ² νννκΈ° μν syntatic sugarμΌ λΏ!
class Person {
// ν΄λμ€ λ°λ μμλ λ©μλλ§μ΄ λ€μ΄κ°λ€. constructor()λ λ©μλμ΄λ€.
}
class
μμμλ function ν€μλλ₯Ό μ°μ§ μλλ€. method()
μ μ¬μ€ method: function ()
μ μ§§κ² μ€μΈ κ²μ΄λ€.constructor
λ©μλclass Tea {
constructor(name, price) {
this.name = name
this.price = price
}
}
// κ°μ²΄ μμ±νκΈ°
var earlGray = new Tea("Earl gray", 3000)
__init__()
μ κ°λ€. π React μμ constructor()
μλ΅νκΈ°
constructor()
μμ this.state
λ₯Ό μ μνμ§ μκ³ ν΄λμ€ λ°λ μμμ λ°λ‘ state
λ‘ μ μνλ κ²½μ°κ° μλ€. babelμ property initializer κΈ°λ₯ λλ¬Έμ΄λ€. transform-class-properties
babel pluginμ μ¬μ©νλ€λ©΄ constructor()
λ₯Ό νΈμΆνμ§ μκ³ λ this
μ μ κ·Όν μ μλ€. μ΄λ¬ν ν¨ν΄μ λ κΉλν μ½λλ₯Ό μν΄ κΆμ₯λλ©° λ리 μ¬μ©λκ³ μλ€.// before
class App extends React.Component {
constructor() {
super()
this.state = { ... }
this.doSomething = this.doSomething.bind(this)
}
doSomething() { ... }
render() {
return ...
}
}
// after
class App extends React.Component {
state = { ... } // stateλ₯Ό constructor μμ μ°μ§ μμλ λλ€!
doSomething = () => { ... } // this λ°μΈλ©μ νμ§ μμλ λλ€!
render() {
return ...
}
}
π μ arrow functionμ this λ°μΈλ©μ νμ§ μμλ λ κΉ?
super()
λΆλͺ¨ κ°μ²΄μ ν¨μλ₯Ό νΈμΆν λ μ¬μ©νλ€.
super(args)
: λΆλͺ¨ κ°μ²΄μ μμ±μ ν¨μ νΈμΆsuper.func(args)
: λΆλͺ¨ κ°μ²΄μ func()
λ©μλ νΈμΆclass Polygon {
constructor(width, height) {
this.width = width
this.height = height
}
get area() {
return this.width * this.height
}
}
class Square extends Polygon {
constructor(sideLength, color) {
super(sideLength, sideLength) // Polygon μ constructorλ₯Ό κ·Έλλ‘ μ¬μ©νλ€.
this.color = color // Polygon μ constructorμ λν΄μ μλ‘μ΄ propertyλ₯Ό μ ν μλ μλ€.
}
}
var square = new Square(2)
console.log(square.area) // 4 => Polygonμ get area() λ©μλλ₯Ό μ¬μ©ν μ μλ€.
constructor()
μμμλ super()
λ₯Ό νΈμΆνκΈ° μ κΉμ§ this
μ μ κ·Όν μ μλ€. (μΆμ²) super()λ₯Ό μ¨μ£Όμ§ μκ³ thisμ μ κ·Όνλ©΄ μλμ κ°μ μ€λ₯κ° λ°μνλ€.> Must call super constructor in derived class before accessing 'this' or returning from derived constructor
static
λ©μλν΄λμ€ λ¨μμ λ°λ‘ νΈμΆνλ λ©μλ.
static
λ©μλλ₯Ό μ¬μ©ν μ μλ€. λ°λλ‘ ν΄λμ€λ λ©€λ² ν¨μλ₯Ό λ°λ‘ μΈ μ μλ€. (μΈμ€ν΄μ€λ₯Ό μμ±νκ³ μΈμ€ν΄μ€ λ¨μμ μ€νν΄μΌ νλ€.)class A {
static sum(a, b) {
return a + b;
}
sub(a, b) {
return a - b;
}
}
// λ©€λ² λ©μλ
var a = new A()
a.sub(2, 1)
// static λ©μλ
A.sum(1, 2)
extends
class Tea {
constructor(name, price) {
this.name = name
this.price = price
}
}
class Coffee extends Tea {
constructor(name, price) {
this.name // Uncaught ReferenceError μ€λ₯ λ°μ (Must call super constructor in derived class before accessing 'this' or returning from derived constructor)
super(name, price, color) // super() λ‘ μμ±μ ν¨μ νΈμΆν΄μΌ thisμ μ κ·Όν μ μμ.
this.color = color
}
}
π λ μ½μ 거리 - μμ±μ ν¨μλ‘ μμ ꡬννκΈ°
extends
class App extends React.Component {
constructor() {
super() // Reactμμ `constructor() {}` λ©μλλ μΈμ λ κ·Έ μμμ `super()`λ©μλλ₯Ό λΆλ¬μΌ νλ€. μ΄κ±Έ μ°μ§ μμΌλ©΄ `missing super() call in constructor` μλ¬κ° λλ€.
this.state = {...}
}
componentDidMount() {
this.setState({...}) // `React.Component`λ₯Ό μμλ°μκΈ° λλ¬Έμ κ·Έ μμ μ μλμ΄ μλ `.setState()`λ©μλλ₯Ό μ¬μ©ν μ μλ€.
}
render() {
return ...
}
}
μ°Έκ³