2023/12/14(λͺ©)
ν΄λμ€μ ν¨μμ κ΄κ³
ν΄λμ€λ ES6μμ λμ¨ κ°λ
μΌλ‘,ES6 μ΄μ μλ νλ‘ν νμ
μ νμ©ν΄
ν΄λμ€μ μλ λ°©μμ λμΌνκ² κ΅¬νν μ μμλ€.
ν΄λμ€κ° μλνλ λ°©μμ μλ°μ€ν¬λ¦½νΈμ νλ‘ν νμ
μ νμ©νλ κ²μ΄λΌκ³ λ³Ό μ μλ€.ν΄λμ€μ μ½λλ₯Ό λ°λ²¨ μμ νΈλμ€νμΌνλ©΄ ν΄λμ€ μλμ μμ±μ ν¨μλ‘
λ§€μ° μ μ¬νκ² μ¬νν μ μλ€.
μ¦, ν΄λμ€λ κ°μ²΄μ§ν₯ μΈμ΄λ₯Ό μ¬μ©νλ λ€λ₯Έ νλ‘κ·Έλλ¨Έκ° μ’ λ μλ°μ€ν¬λ¦½νΈ μ μ κ·ΌνκΈ° μ½κ² λ§λ€μ΄μ£Όλ, μΌμ’
μ λ¬Έλ²μ μ€ν(Syntacic sugar)μ μν μ νλ€κ³ λ³Ό μ μλ€.λν μλ°μ€ν¬λ¦½νΈ ν΄λμ€κ° νλ‘ν νμ
μ κΈ°λ°μΌλ‘ μλνλ€. μλ μμ μ½λλ ν΄λμ€μ μ½λλ₯Ό λ°λ²¨μμ μμ±μ ν¨μλ‘ λ³νν μ½λλ€.
<script>
class Car {
constructor(name) {
this.name = name
}
honk() {
console.log(`${this.name}μ΄ κ²½μ μ μΈλ¦½λλ€!`)
}
static hello() {
console.log('μ λ μλμ°¨μ
λλ€')
}
set age(value) {
this.carAge = value
}
get age() {
return this.carAge
}
}
'use strict'
//ν΄λμ€κ° ν¨μμ²λΌ νΈμΆλλ κ²μ λ°©μ§
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
// νλ‘νΌν°λ₯Ό ν λΉνλ μ½λ
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i]
descriptor.enumerable = descriptor.enumerable || false
descriptor.configurable = true
if ("value" in descriptor) descriptor.writable = true
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
}
}
// νλ‘ν νμ
λ©μλμ μ μ λ©μλλ₯Ό μ μΈνλ μ½λ
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps)
if (staticProps) _defineProperties(Constructor, staticProps)
Object.defineProperty(Constructor, "prototype", { writable: false })
return Constructor
}
var Car = /*#__PURE__*/function () {
function Car(name) {
_classCallCheck(this, Car);
this.name = name;
}
_createClass(Car, [{
key: "honk",
value: function honk() {
console.log("".concat(this.name, "\uC774 \uACBD\uC801\uC744 \uC6B8\uB9BD\uB2C8\uB2E4!"));
}
}, {
key: "age",
get: function get() {
return this.carAge;
},
set: function set(value) {
this.carAge = value;
}
}], [{
key: "hello",
value: function hello() {
console.log('μ λ μλμ°¨μ
λλ€');
}
}]);
return Car;
}();
</script>
ν΄λμ€λ κ°μ²΄λ₯Ό μμ±νκΈ° μν΄ λμμ΄ λλ μ¬λ¬ κΈ°λ₯μ μ 곡νκ³ μλ€.
리μ‘νΈμ λ§μ μ½λλ€μ΄ ν΄λμ€ν μ»΄ν¬λνΈλ‘ μμ±λμΌλ―λ‘ ν΄λμ€λ₯Ό μ΄ν΄νκ³ λλ©΄ ν΄λμ€ν μ»΄ν¬λνΈμ μ΄λ»κ² μλͺ
μ£ΌκΈ°λ₯Ό ꡬνν μ μλμ§, μ ν΄λμ€ν μ»΄ν¬λνΈ μμ±μ μν΄ React.Componentλ React.PureComponentλ₯Ό μμνλμ§,λ©μλκ° νμ΄ν ν¨μμ μΌλ° ν¨μμΌ λ μ΄λ€ μ°¨μ΄κ° μλμ§ λ±μ μ΄ν΄ν μ μμκ²μ΄λ€.
β‘οΈ λ€μ κΈ μμλ ν΄λ‘μ μ λν΄ μ΄ν΄λ³΄μ.