μ€λ 곡λΆνλ©΄μ μΈμ¬μ΄νΈ, κ°λ μμ½ λ±
νμ μμ±μμ νλ‘ν νμ μ μμ μμ±μμ κ°μ²΄λ‘ μ§μ
μμ μμ±μμ λͺ¨λ μμ±μ λ¬Όλ €λ°μ μ¬μ©ν μ μμ
μμ ex03-04.js
μ€λ³΅μμ±
/**
* κ³ λ±νκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ κ³Ό νκ· κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function HighSchool(kor, eng) {
// μμ± μ΄κΈ° ν
this.kor = kor;
this.eng = eng;
}
HighSchool.prototype.sum = function () {
return this.kor + this.eng;
};
HighSchool.prototype.avg = function () {
return this.sum() / 2;
};
const s1 = new HighSchool(100, 91);
console.log("κ³ λ±νκ΅ μ΄μ ", s1.sum());
console.log("κ³ λ±νκ΅ νκ· ", s1.avg());
/**
* λνκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ , νκ· κ³Ό νμ κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function College(kor, eng) {
this.kor = kor;
this.eng = eng;
}
College.prototype.sum = function () {
return this.kor + this.eng;
};
College.prototype.avg = function () {
return this.sum() / 2;
};
College.prototype.grade = function () {
const avg = this.avg();
if (avg >= 90) {
return "A";
} else if (avg >= 80) {
return "B";
} else if (avg >= 70) {
return "C";
} else if (avg >= 60) {
return "D";
} else {
return "F";
}
};
const c1 = new College(80, 91);
console.log("λνκ΅ μ΄μ ", c1.sum());
console.log("λνκ΅ νκ· ", c1.avg());
console.log("λνκ΅ νμ ", c1.grade());
μμ λ°λ μ½λ
/**
* κ³ λ±νκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ κ³Ό νκ· κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function HighSchool(kor, eng) {
// μμ± μ΄κΈ° ν
this.kor = kor;
this.eng = eng;
}
HighSchool.prototype.sum = function () {
return this.kor + this.eng;
};
HighSchool.prototype.avg = function () {
// μμ 첫째μ리μμ λ°μ¬λ¦Όνλ€
return Math.round(this.sum() / 2);
};
const s1 = new HighSchool(100, 91);
console.log("κ³ λ±νκ΅ μ΄μ ", s1.sum());
console.log("κ³ λ±νκ΅ νκ· ", s1.avg());
/**
* λνκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ , νκ· κ³Ό νμ κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function College(kor, eng) {
this.kor = kor;
this.eng = eng;
}
// Collegeκ° HighSchoolμ μμ λ°λλ€
College.prototype = new HighSchool();
College.prototype.**constructor** = College;
College.prototype.grade = function () {
const avg = this.avg();
if (avg >= 90) {
return "A";
} else if (avg >= 80) {
return "B";
} else if (avg >= 70) {
return "C";
} else if (avg >= 60) {
return "D";
} else {
return "F";
}
};
const c1 = new College(80, 91);
console.log("λνκ΅ μ΄μ ", c1.sum());
console.log("λνκ΅ νκ· ", c1.avg());
console.log("λνκ΅ νμ ", c1.grade());
gpt μ€λͺ
HighSchool: μ μ β sum(), avg() κ³μ°λ§ λ΄λΉCollege: HighSchool κΈ°λ₯ + grade() νμ₯College.prototype = new HighSchool() λ₯Ό ν΅ν΄ μμ ꡬνconstructorλ λ€μ Collegeλ‘ λλλ €μ€μΌ μ νν¨constructorλ?
π§ constructorλ?
constructorλ κ°μ²΄μ νλ‘ν νμ
(prototype) κ°μ²΄μ κΈ°λ³ΈμΌλ‘ ν¬ν¨λ μμ±μ΄μΌ..prototype.constructor μμ±μ κ°μ§κ³ μμ΄.μμ:
function HighSchool(kor, eng) {
this.kor = kor;
this.eng = eng;
}
console.log(HighSchool.prototype.constructor === HighSchool); // β
true
μ΄ λ§μ μ¦,
const s1 = new HighSchool(100, 90);
λ₯Ό νμ λ, s1.constructor === HighSchool μ΄ μ±λ¦½ν΄.
μλλ©΄ s1.__proto__ === HighSchool.prototype μ΄κ³ , κ·Έ μμ μλ .constructorκ° HighSchoolμ κ°λ¦¬ν€λκΉ!
π κ·Όλ° μμν λ λ¬Έμ κ° μ겨!
College.prototype = new HighSchool();
μ΄λ κ² μμνλ©΄ College.prototypeμ μ΄μ HighSchoolμ μΈμ€ν΄μ€κ° λλκΉ,
College.prototype.constructorλ HighSchoolμ κ°λ¦¬ν€κ² λΌ.
console.log(College.prototype.constructor === HighSchool); // β μ°λ¦¬κ° μνλ κ² μλ
κ·Έλμ μλμ μμ±μ μ λ³΄κ° κΌ¬μ΄κ² λκ³ , μ΄κ±Έ λͺ μμ μΌλ‘ λ€μ μ€μ ν΄μ€μΌ ν΄:
College.prototype.constructor = College;
μ΄λ κ² νλ©΄:
const c1 = new College(80, 91);
console.log(c1.constructor === College); // β
true
π μ constructorλ₯Ό λ°λ₯΄κ² μ§μ ν΄μΌ ν κΉ?
constructor κ°μ λ³΄κ³ νμ
λΆκΈ° μ²λ¦¬νκΈ°λ ν¨π μμ½
.constructorλ μ΄λ€ μμ±μ ν¨μλ‘ λ§λ€μ΄μ‘λμ§ μλ €μ£Όλ μμ±prototype = new Parent() νλ©΄ .constructorκ° κΌ¬μ΄λ―λ‘ constructorλ₯Ό μ§μ μ¬μ§μ ν΄μ€μΌ ν¨μμ ex03-04.jsλ₯Ό ex03-05.jsλ‘
HighSchool β College μμ νλ¦ μκ° μ 리
/* ex03-04 */
/**
* κ³ λ±νκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ κ³Ό νκ· κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function HighSchool(kor, eng) {
// μμ± μ΄κΈ° ν
this.kor = kor;
this.eng = eng;
}
HighSchool.prototype.sum = function () {
return this.kor + this.eng;
};
HighSchool.prototype.avg = function () {
// μμ 첫째μ리μμ λ°μ¬λ¦Όνλ€
return Math.round(this.sum() / 2);
};
const s1 = new HighSchool(100, 91);
console.log("κ³ λ±νκ΅ μ΄μ ", s1.sum());
console.log("κ³ λ±νκ΅ νκ· ", s1.avg());
{...}
console.log(College.prototype);
// Collegeμ νλ‘ν νμ
μΆλ ₯
/* ex03-05 */
{...}
/**
* λνκ΅ μ±μ κ΄λ¦¬ μμ±μ ν¨μ(μ΄μ , νκ· κ³Ό νμ κ³μ°)
* @param {number} kor κ΅μ΄ μ μ
* @param {number} eng μμ΄ μ μ
*/
function College(kor, eng) {
this.kor = kor;
this.eng = eng;
}
// Collegeκ° HighSchoolμ μμ λ°λλ€
function inherite(Parent, Child) {
// 첫λ²μ§Έ λ°©λ²
const F = new Function();
F.prototype = HighSchool.prototype; // Fμ νλ‘ν νμ
μ HighSchoolμ νλ‘ν νμ
μΌλ‘ λ³κ²½
College.prototype = new F(); // FμΈμ€ν΄μ€ μμ±ν Collegeμ νλ‘ν νμ
μΌλ‘ λμ
νλ€
College.prototype.constructor = College;
// 2λ²μ§Έ λ°©λ²
Child.prototype = Object.create(Parent.prototype);
//createν¨μλ μ§μ ν prototype κ°μ²΄λ₯Ό μ°Έμ‘°νλ μΈμ€ν΄μ€ μμ±
Child.prototype.constructor = Child;
// μμ μμ±μμ constructor νλ‘νΌν° 볡μ
}
inherite(HighSchool, College);
College.prototype.grade = function () {
const avg = this.avg();
if (avg >= 90) {
return "A";
} else if (avg >= 80) {
return "B";
} else if (avg >= 70) {
return "C";
} else if (avg >= 60) {
return "D";
} else {
return "F";
}
};
const c1 = new College(80, 91);
console.log("λνκ΅ μ΄μ ", c1.sum());
console.log("λνκ΅ νκ· ", c1.avg());
console.log("λνκ΅ νμ ", c1.grade());
console.log(College.prototype);
// Collegeμ νλ‘ν νμ
μΆλ ₯
Class μ¬μ©ECMAScript6(2015)μ μΆκ°λ ν€μλ
κ°μ²΄μ§ν₯ μΈμ΄μ classμ λΉμ·ν λ°©μμΌλ‘ μμ±μ ν¨μλ₯Ό κΈ°μ
κ°μ²΄λ₯Ό μμ±νκ³ prototype κΈ°λ°μ μμμ λ³΄λ€ λͺ λ£νκ² νν
classλ μ¬μ€ **ν¨μ**μ΄λ©° class μ μΈλ¬Έκ³Ό class ννμ λ°©μμΌλ‘ μ¬μ©
class μ μΈλ¬Έ
// κΈ°λ³Έ νμ
class ν΄λμ€λͺ
{...}
// μμ
class hi {...}
// κΈ°λ³Έ νμ
const λ³μ = class {...}
// μμ
const hi = class {...}
Class bodyμ λ©μλ μ μ
ν΄λμ€μ λ°λμ ν΄λμ€ λ©€λ²λ³μ(μμ±)μ λ©μλ μ μ
λ©€λ²λ³μμ μ΄κΈ°νλ Constructor λ©μλμ μ μ
Constructor λ©μλ(μμ±μ)
// β
Class λ¬Έλ² (ES6)
class HighSchool {
constructor(kor, eng) {
this.kor = kor;
this.eng = eng;
}
}
const s1 = new HighSchool(100, 90);
console.log(s1.kor, s1.eng);
// β
Function μμ±μ λ°©μ
function HighSchool(kor, eng) {
this.kor = kor;
this.eng = eng;
}
const s1 = new HighSchool(100, 90);
console.log(s1.kor, s1.eng);
ν΄λ‘μ λ
μ€νμ΄ λλ μΈλΆ ν¨μμ λ³μμ μ κ·Όν μ μλ λ΄λΆ ν¨μ
ν¨μκ° μμ±λλ μμ μ κΈ°μ€μΌλ‘ μ κ·Ό κ°λ₯ν λ³μλ, κ·Έ μ ν¨λ²μκ° μ¬λΌμ§ νμλ μ κ·Ό κ°λ₯
ν΄λ‘μ λ‘ μΈν΄ μ ν¨ λ²μκ° μ¬λΌμ§ λ³μμ ν¨μλ₯Ό μ¬μ©ν μ μκ³ , λ³μμ κ²½μ° κ·Έ κ°μ λ³κ²½ν μλ μλ€.
ex04-01.js
const topLevel = "μ΅μμ λ³μ";
function outer() {
const innerVal = "outerμ μ§μλ³μ";
console.log(topLevel); //=> 1οΈβ£
console.log(innerVal); //=> 2οΈβ£
}
outer();
console.log(topLevel); //=> 3οΈβ£
console.log(innerVal); //=> 4οΈβ£
/* μΆλ ₯ μμ
1οΈβ£ 2οΈβ£ 3οΈβ£ (4οΈβ£λ μλ¬)
*/
topLevelμ ν¨μ λ΄λΆμμλ μ κ·Ό κ°λ₯νλ€.innerValμ outer ν¨μ λ΄λΆμμ μ μΈλ μ§μ λ³μμ΄λ€.topLevelμ μ μ λ³μμ΄λ―λ‘ ν¨μ μΈλΆμμλ λ¬Έμ μμ΄ μ κ·Όλ¨.innerValμ outer ν¨μκ° μ’
λ£λλ©΄ μ¬λΌμ§λ μ§μ λ³μμ΄λ€.ReferenceErrorκ° λ°μνκ² λλ€.const topLevel = "μ΅μμ λ³μ";
function outer() {
const innerVal = "outerμ μ§μλ³μ";
console.log(topLevel); // 1οΈβ£
console.log(innerVal); // 2οΈβ£
return innerVal;
}
outer();
console.log(topLevel); // 3οΈβ£
console.log(innerVal); // 4οΈβ£
/* μΆλ ₯μμ
1οΈβ£ 2οΈβ£ 3οΈβ£ (4οΈβ£λ μλ¬)
*/
topLevelμ ν¨μ μΈλΆμμ μ μΈλ μ μ λ³μμ΄λ©°, ν¨μ λ΄λΆμμ μλμΌλ‘ μ°Έμ‘°λλ€. μλ°μ€ν¬λ¦½νΈμ μ€μ½ν μ²΄μΈ λλΆμ νμ μ€μ½ν μμλ μμ μ€μ½νμ μλ λ³μμ μ κ·Όν μ μκ² λλ€.innerValμ outer ν¨μ μμμ μ μΈλ μ§μ λ³μμ΄κΈ° λλ¬Έμ ν΄λΉ ν¨μ λ΄λΆμμλ§ μ ν¨νλ€. μ§μ μ€μ½νλ ν¨μ μ€ν μ μκΈ°λ©° ν¨μ μ’
λ£μ ν¨κ» λ©λͺ¨λ¦¬μμ μ κ±°λλ€.topLevelμ μ μμ μ μΈλμ΄ μκΈ° λλ¬Έμ μ΄λμλ μ κ·Ό κ°λ₯νλ€.console.log(topLevel)λ μ ν λ¬Έμ μμ΄ μ€νλλ€.innerValμ outer λ΄λΆμμ μ μΈλμκΈ° λλ¬Έμ μΈλΆμμλ μ°Έμ‘°ν μ μλ€. ν¨μ λ°μμ μ κ·Όνλ €κ³ νλ©΄ ReferenceError λ°μ.const topLevel = "μ΅μμ λ³μ";
function outer() {
const innerVal = "outerμ μ§μλ³μ";
console.log(topLevel); // => 1οΈβ£
console.log(innerVal); // => 2οΈβ£
const innerFn = function () {
console.log(innerVal); // => 3οΈβ£
};
innerFn();
}
outer();
console.log(topLevel); // => 4οΈβ£
console.log(innerVal); // => 5οΈβ£
/* μΆλ ₯ μμ
1οΈβ£ 2οΈβ£ 3οΈβ£ 4οΈβ£ (5οΈβ£λ μλ¬)
*/
innerVal μ outerν¨μκ° μ’
λ£λλ©΄ λ©λͺ¨λ¦¬μμ μ κ±°λλ―λ‘ μ€λ₯.const topLevel = "μ΅μμ λ³μ";
function outer() {
const innerVal = "outerμ μ§μλ³μ";
console.log(topLevel); // => 1οΈβ£
console.log(innerVal); // => 2οΈβ£
const innerFn = function () {
console.log(innerVal); // => 3οΈβ£
};
return innerFn;
}
const innerFn = outer();
console.log(topLevel); // => 4οΈβ£
// console.log(innerVal); // => 5οΈβ£
innerFn(); // => 6οΈβ£
/* μΆλ ₯ μμ
1οΈβ£ 2οΈβ£ 4οΈβ£ 6οΈβ£ (5οΈβ£λ μ£Όμ μ²λ¦¬)
*/
innerVal μ μΈλΆμμ μ κ·Όν μ μμ΄ μ£Όμ ν΄μ μ μ€λ₯ λ°μνλ€.innerFnμ outerμ μ§μλ³μλ₯Ό κΈ°μ΅νλ ν΄λ‘μ λ‘ λμνλ€.μ¬λ¬κ°μ μΈμλ₯Ό λ°λ ν¨μλ₯Ό λ¨μΌ μΈμλ₯Ό λ°λ ν¨μμ 체μΈμΌλ‘ νΈμΆνλλ‘ λ°κΎΈλ ν¨μν νλ‘κ·Έλλ° κΈ°λ² μ€ νλ
sum(x, y) -> sum(x)(y)ν¨μν νλ‘κ·Έλλ° μΈμ΄μ λ§μ 곡νμ ν λ―Έκ΅μ μνμ, λ Όλ¦¬νμμΈ νμ€μΌ 컀리μ μ΄λ¦μμ λ°μ΄
ν¨μμ κ°λ
μ±, μ¬μ¬μ©μ΄ μ’μμ§.
λ§μ§λ§ μΈμκ° μ λ ₯λ λκΉμ§ ν¨μμ μ€ν νμ΄λ°μ μ‘°μ ν μ μμ.
ex04-03.js
function sum(a, b, c) {
return a + b + c;
}
console.log(sum(10, 20, 30));
// ν¨μ μ μΈμ
let currySum = function (a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
};
// νμ΄ν ν¨μ
currySum = (a) => (b) => (c) => a + b + c;
// μ€ν ν μ½λκ° a, b, c κ°κ° νλμ΄κΈ° λλ¬Έμ
// μ€κ΄νΈ μλ΅ κ°λ₯ν¨
console.log(currySum(10)(20)(30));
π μ 리λ νΈμΆ ꡬ쑰
currySum(10)
βββ> returns (b) => (c) => a + b + c
currySum(10)(20)
βββ> returns (c) => a + b + c
currySum(10)(20)(30)
βββ> returns 60
ex04-04.js, index.html
<script src="https://unpkg.com/lodash@4.17.21/lodash.js"></script>
lodash λΌμ΄λΈλ¬λ¦¬μ μ νΈ ν¨μλ€μ μΉμμ λ°λ‘ μ¬μ©νκΈ° μν΄ μΆκ°νλ€. β
const sum = function (x, y) {
return x + y;
};
const currySum = _.curry(sum);
console.log(sum(10, 20));
console.log(currySum(30)(40));
μμ 03μμ μ΄μ¬ν ν¨μλ₯Ό 리ν΄νλ λ°©μμΌλ‘ 컀λ§μ μ§μ λ§λ€μμ§λ§,
lodashμ _.curryλ₯Ό μ°λ©΄ μ΄λ° μ½λλ₯Ό μλμΌλ‘ μ»€λ§ ν¨μλ‘ λ³νν΄μ€λ€.
μΊ‘μν
κ°μ²΄ λ΄λΆμμλ§ μ κ·Ό κ°λ₯ν μμ±μ λ§λ€μ΄ μ¬μ©νκ³ μΈλΆμμλ ν΄λΉ μμ±μ μ§μ μ κ·Όνμ§ λͺ»νλλ‘ λ§λλ κ°μ²΄μ§ν₯ μΈμ΄μ νΉμ§(C++, Javaμμλ private ν€μλλ‘ μ§μ κ°λ₯)
ν¨μ λ΄λΆμμ μ μΈν μ§μλ³μλ μΈλΆμμ μ κ·Όνμ§ λͺ»νλ λ°λ©΄ λ΄λΆ λ©μλμΈ ν΄λ‘μ μμλ μ κ·Ό κ°λ₯νλ€λ νΉμ§μ μ΄μ©ν΄μ ꡬν
ES2019μμ class μ μμ μμ±λͺ μ΄λ λ©μλλͺ μμ #μ λΆμ΄λ©΄ ν΄λΉ class λ΄λΆμμλ§ μ κ·Ό κ°λ₯ν private μμ±κ³Ό λ©μλ μ μ κΈ°λ₯μ΄ μΆκ°λ¨
ex04-02.js
// count μμ±κ³Ό ride(), getCount() λ©μλ μμ±
const Counter = function () {
this.count = 0;
this.getCount = function () {
return this.count;
};
this.ride = function () {
this.count++;
};
};
const c = new Counter();
c.ride();
c.ride();
c.count += 40;
console.log("μ 체 νμΉμ", c.getCount());
β νμ¬ μ½λμ μ£Όμ λ¬Έμ μ
countλ₯Ό μΈλΆμμ μ§μ μμ κ°λ₯ν¨ (μΊ‘μν μλ°)countλ₯Ό μ§μ μ‘°μν μ μμ΄:c.count += 40;ride() λ©μλλ₯Ό μ°νν΄μ κ°μ λ³κ²½νλ κ² κ°λ₯νλκΉ,λ΄λΆ λ‘μ§μ μ λ’°μ±μ κΉ¨λ¨λ¦¬κ² λΌ.c.count = -999μ²λΌ λ°κΎΈλ©΄ μ 체 λ‘μ§μ΄ 무λμ§.ride() λ©μλλ countλ₯Ό 1μ© μ¦κ°μν€λ μν μΈλ°,μΈλΆμμ μ§μ countλ₯Ό λ°κΏλ²λ¦¬λ©΄ ride() νΈμΆ μ¬λΆμ 무κ΄ν΄μ§.// κ³μ μ
// count μμ±κ³Ό ride(), getCount() λ©μλ μμ±
const Counter = function () {
let count = 0;
this.getCount = function () {
return count;
};
this.ride = function () {
if (count < 40) {
count++;
} else {
console.log("μ μμ΄ μ΄κ³Όλμμ΅λλ€.!");
}
};
};
const c = new Counter();
c.ride();
c.ride();
for (let i = 0; i < 40; i++) {
c.ride();
}
console.log("μ 체 νμΉμ", c.getCount());
β κ°μ λ μ
countκ° ν΄λ‘μ λ‘ λ³΄νΈλ¨ (μλν μ±κ³΅)let count = 0;μ΄ μμ±μ ν¨μ λ΄λΆμ μ μΈλμ΄,μΈλΆμμλ countμ μ§μ μ κ·Όνκ±°λ μμ ν μ μμ.c.count += 40 κ°μ μ‘°μμ λΆκ°λ₯νκ³ , μ€μ§ λ©μλλ‘λ§ μ‘°μ κ°λ₯ν¨.forλ¬Έμμ let i = 0μ μ¬μ©ν΄, iκ° λΈλ‘ μ€μ½ν λ³μκ° λμμ.iκ° μ μ λ³μλ‘ μ묡 μ μΈλμ΄ λ€λ₯Έ μ½λμ μν₯μ μ€ μ μμμ§λ§,μ΄μ μμ νκ² ν΄λΉ λΈλ‘ μμμλ§ λμν¨.ride() λ©μλμ count < 40 μ‘°κ±΄μ΄ μ겨, μ΅λ νμΉ μΈμ μ ν κ°λ₯."μ μμ΄ μ΄κ³Όλμμ΅λλ€.!" λ©μμ§λ₯Ό μΆλ ₯νκ³ μ¦κ°νμ§ μμ.Partial application
κΈ°μ‘΄ ν¨μμ λ§€κ°λ³μ μ€ μΌλΆλ₯Ό 미리 μ±μλ μνμ ν¨μ
컀λ§λ ν¨μλ₯Ό μΌλΆ λ¨κ³κΉμ§λ§ νΈμΆν ν λ°νλ°μ ν¨μλ₯Ό λμ€μ λλ¨Έμ§ μΈμλ₯Ό μ λ¬ν΄μ μ€ν
ex02-17-05.js
// κΈ°μ‘΄ ex02-17-05.js
var count = 0;
const myObj = {
count: 0,
visit: function(){
// λ°©λ¬Έμλ₯Ό νλͺ
μ¦κ°μν¨λ€.
this.count++; // this = myObj
const visit2 = function(){
this.count++; // this = window
};
visit2();
},
};
myObj.visit(); // this = myObj
myObj.visit();
console.log('λ°©λ¬Έμμ', myObj.count); // 2
// λ³κ²½λ ex02-17-05.js
var count = 0;
const myObj = {
count: 0,
visit: function () {
// λ°©λ¬Έμλ₯Ό νλͺ
μ¦κ°μν¨λ€.
this.count++; // this = myObj
const visitN = function (n) {
this.count += n; // this = window
};
visitN.call(this, 2); // countλ₯Ό 2λͺ
μ¦κ° => 1 + 2 = 3
visitN.call(this, 2); // countλ₯Ό 2λͺ
μ¦κ° => 3 + 2 = 5
const visit3 = visitN.bind(this, 3);
visit3(); // conutλ₯Ό 3λͺ
μ¦κ° => 5 + 3 = 8
visit3(); // conutλ₯Ό 3λͺ
μ¦κ° => 8 + 3 => 11
},
};
myObj.visit(); // this = myObj => 11
myObj.visit();
// 11 β 12(11 + 1) β 14(12 + 2) β 16(14 + 2) β 19(16 + 3) β 22(19 +3)
console.log("λ°©λ¬Έμμ", myObj.count); // 22
β μ΄λ»κ² λ°λμλκ°?
| νλͺ© | μΌμͺ½ μ½λ | μ€λ₯Έμͺ½ μ½λ |
|---|---|---|
| λ΄λΆ ν¨μ μ΄λ¦ | visit2 (λ§€κ°λ³μ μμ) | visitN (λ§€κ°λ³μ n μ¬μ© κ°λ₯) |
| νΈμΆ λ°©μ | κ·Έλ₯ νΈμΆ (visit2()) | call, bind μ¬μ©ν΄ this λͺ
ννκ² μ€μ |
| this λ°μΈλ© λ¬Έμ | λ΄λΆ ν¨μμμ thisλ window (λλ undefined) | call, bindλ‘ thisλ₯Ό λͺ
μμ μΌλ‘ myObjλ‘ μ€μ |
| μ μ°μ± | λ°©λ¬Έμ 1λͺ μ© μ¦κ°λ§ κ°λ₯ | nλͺ
μ© μ λμ μΌλ‘ μ¦κ° κ°λ₯ |
| μ¬μ¬μ©μ± | μμ | μμ β ν¨μ μ¬μ¬μ© κ°λ₯ (visitN, visit3) |
π‘ μ΄λ€ μ μ΄ μ’μκ°?
visit2()λ₯Ό μΌλ° ν¨μμ²λΌ νΈμΆν΄μ thisκ° myObjκ° μλcall, bindλ₯Ό μ¬μ©ν΄ thisλ₯Ό myObjλ‘ κ³ μ μμΌ λ¬Έμ ν΄κ²°λ¨nμ μ λ¬ν μ μμ΄, 1λͺ
, 2λͺ
, 3λͺ
λ± μμ λ‘κ² μ€μ κ°λ₯ β visitN(this, 2) β λ°©λ¬Έμ 2λͺ
μ¦κ°visitNμ λ²μ© ν¨μλ‘ μ¬λ¬ μν©μμ μ¬μ¬μ© κ°λ₯bindλ‘ λ―Έλ¦¬ this κ³ μ ν΄ λ§λ visit3μ λμ€μ λ°λ³΅ νΈμΆνκΈ°λ μ¬μthis λ°μΈλ© λ¬Έμ λ₯Ό ν΄κ²°νκ³ , λ§€κ°λ³μλ₯Ό λ°μ μ μ°νκ³ μ¬μ¬μ© κ°λ₯ν ν¨μ κ΅¬μ‘°λ‘ κ°μ ν μ μ΄ ν΅μ¬!β
bind()λ?
bind()λ ν¨μμ this κ°μ κ³ μ ν΄μ μλ‘μ΄ ν¨μλ₯Ό λ°ννλ λ©μλμΌ.
λ¨, λ°λ‘ μ€νλμ§λ μκ³ κ³ μ λ thisλ₯Ό κ°μ§ μ ν¨μλ§ λ¦¬ν΄ν΄!
π§ ν΅μ¬ κ°λ μ 리
bind()λ κΈ°μ‘΄ ν¨μλ₯Ό 볡μ νλ©΄μ thisλ₯Ό μ§μ ν ν¨μλ‘ λ§λ¦
μλ³Έ ν¨μλ λ³νμ§ μμ
μΈμ μ°λλ©΄?
thisκ° λ°λμ§ μλλ‘ λ―Έλ¦¬ κ³ μ νκ³ μΆμ λthisκ° κΌ¬μ΄λ κ±Έ λ§κΈ° μν΄π νμ€ μμ½ (κ°ν λ²μ )
bind()λ ν¨μμ thisλ₯Ό νΉμ κ°μ²΄λ‘ κ³ μ ν μλ‘μ΄ ν¨μλ₯Ό λ°νν΄, λμ€μ νΈμΆν λλ thisκ° λ°λμ§ μλλ‘ λ³΄μ₯νλ€.
λ©λͺ¨μ΄μ μ΄μ
ν΄λ‘μ κ° νμ μμ
λ©λͺ¨μ΄μ μ΄μ μμ
ex02-23.js, ex04-05.js
// ex02-23.js
const isPrime = function (num) {
isPrime._cache = isPrime._cache || {};
if (isPrime._cache[num] !== undefined) {
return isPrime._cache[num];
}
let prime = "λ§μ";
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
prime = "μλ";
break;
}
}
isPrime._cache[num] = prime;
return prime;
};
console.time("μμμκ°");
console.log("3 -> ", isPrime(3));
console.log("4 -> ", isPrime(4));
console.log("5 -> ", isPrime(5));
console.log("6 -> ", isPrime(6));
console.log("7 -> ", isPrime(7));
console.log("8 -> ", isPrime(8));
console.log("9 -> ", isPrime(9));
console.log("1000000007 -> ", isPrime(1000000007));
console.log("1000000007 -> ", isPrime(1000000007));
console.log("1000000007 -> ", isPrime(1000000007));
console.timeEnd("μμμκ°");
// ex04-05.js
Function.prototype.memo = function (key) {
this._cache = this._cache || {};
if (this._cache[key] !== undefined) {
return this._cache[key];
} else {
return (this._cache[key] = this(key));
}
};
const isPrime = function (num) {
let prime = "λ§μ";
for (let i = 2; i < num; i++) {
if (num % i === 0) {
prime = "μλ";
break;
}
}
return prime;
};
console.time("μμμκ°");
console.log("3 -> ", isPrime(3));
console.log("4 -> ", isPrime(4));
console.log("5 -> ", isPrime(5));
console.log("6 -> ", isPrime(6));
console.log("7 -> ", isPrime(7));
console.log("8 -> ", isPrime(8));
console.log("9 -> ", isPrime(9));
console.log("1000000007 -> ", isPrime.memo(1000000007)); // 첫λ²μ§Έ μΊμ±
console.log("1000000007 -> ", isPrime.memo(1000000007)); // 2λ²μ§Έ μΊμ±
console.log("1000000007 -> ", isPrime.memo(1000000007)); // 3λ²μ§Έ μΊμ±
console.timeEnd("μμμκ°");
isPrime.memo(3)
β
ββ> memo λ©μλ μ€ν
β ββ> μΊμ νμΈ/μ΄κΈ°ν
β ββ> μΊμμ 3μ΄ μμΌλ―λ‘
β β ββ> isPrime(3) μ€ν
β β ββ> μμ νλ³ ν true λ°ν
β ββ> μΊμμ μ μ₯ ν true λ°ν
β
isPrime(3)
ββ> isPrime ν¨μ λ°λ‘ μ€ν
ββ> μμ νλ³ ν true λ°ν
β κ°μ λ μ μμ½
λ©λͺ¨μ΄μ μ΄μ λ‘μ§μ ν¨μ λ°μΌλ‘ λΆλ¦¬ (μ¬μ¬μ© κ°λ₯μ± μ¦κ°)
isPrime ν¨μ μμμ _cache κ΄λ¦¬Function.prototype.memoλ‘ λ©λͺ¨μ΄μ μ΄μ
μ μ νΈλ¦¬ν° ν¨μν β λ€λ₯Έ ν¨μμμλ .memo() νΈμΆλ§μΌλ‘ μΊμ κΈ°λ₯μ κ°νΈνκ² μΈ μ μμκ΄μ¬μ¬μ λΆλ¦¬ (κΈ°λ₯ λΆλ¦¬λ‘ κ°λ μ± ν₯μ)
isPrimeμ μ€μ§ μμ νλ³λ§, memoλ μΊμ±λ§ μ²λ¦¬ν΄μ μ½λ μν μ΄ λͺ
νν λλ¨μ½λ μ¬μ¬μ©μ± μ¦κ°
Function.prototype.memoλ₯Ό λͺ¨λ ν¨μμμ μ¬μ©ν μ μμ:
const slowFn = (x) => { ... };
slowFn.memo(x); // μΊμ± μλ μ²λ¦¬
ν¨μ μ μκ° λ μμν΄μ§ (Pure Functionν)
isPrimeμ μΊμλ μΈλΆ μν μμ΄ μ€μ§ μΈμλ§μΌλ‘ λμμ½λμ νμ₯μ± ν₯μ
memo()μ λ§λ£ μκ°, μΊμ ν¬κΈ° μ ν κ°μ κΈ°λ₯λ μ½κ² μΆκ° κ°λ₯π ν μ€ μμ½
κΈ°λ₯ λΆλ¦¬ + μ¬μ¬μ©μ± + μ μ§λ³΄μμ± + νμ₯μ±κΉμ§ λͺ¨λ μ’μμ§ κ΅¬μ‘°!
μ€λͺ
// ex04-05.js
Function.prototype.memo = function (key) {
this._cache = this._cache || {};
if (this._cache[key] !== undefined) {
return this._cache[key];
} else {
return (this._cache[key] = this(key));
}
};
const isPrime = function (num) {
let prime = "λ§μ";
for (let i = 2; i < num; i++) {
if (num % i === 0) {
prime = "μλ";
break;
}
}
return prime;
};
const sayHello = function (name) {
return "Hello" + name;
};
console.log(sayHello("μΊμ± μν¨"));
console.log(sayHello.memo("μΊμ± ν¨"));
console.log(sayHello.memo("μΊμ± ν¨"));
console.time("μμμκ°");
console.log("3 -> ", isPrime(3));
console.log("4 -> ", isPrime(4));
console.log("5 -> ", isPrime(5));
console.log("6 -> ", isPrime(6));
console.log("7 -> ", isPrime(7));
console.log("8 -> ", isPrime(8));
console.log("9 -> ", isPrime(9));
console.log("1000000007 -> ", isPrime.memo(1000000007));
console.log("1000000007 -> ", isPrime.memo(1000000007));
console.log("1000000007 -> ", isPrime.memo(1000000007));
console.timeEnd("μμμκ°");
1οΈβ£ 1. Function.prototype.memo = function (key) { ... }
π ν΅μ¬ κΈ°λ₯
λͺ¨λ ν¨μμ 곡ν΅μ μΌλ‘ μ¬μ©ν μ μλ λ©λͺ¨μ΄μ μ΄μ κΈ°λ₯μ μΆκ°ν¨
this._cacheλ₯Ό ν΅ν΄ ν¨μ λ¨μλ‘ μΊμ κ°μ²΄λ₯Ό μ μ₯
2οΈβ£ sayHello.memo("μΊμ± ν¨")
π μν
sayHello("μΊμ± ν¨")μ μ΅μ΄ ν λ² κ³μ° ν μΊμμ μ μ₯π§© ν¬μΈνΈ
"μΊμ± ν¨" β λ΄λΆμ μΌλ‘ sayHello._cache["μΊμ± ν¨"]μ μ μ₯λ¨3οΈβ£ isPrime.memo(1000000007)
π μν
μμ νλ³ ν¨μ isPrimeμ λν΄ κ³ λΉμ© κ³μ°μ λ©λͺ¨μ΄μ μ΄μ
μΌλ‘ μ΅μ ν
1000000007μ λ§€μ° ν° μμ΄λ―λ‘ μ²« κ³μ°μ μκ°μ΄ μ€λ κ±Έλ¦Ό
μ΄ν λμΌν μ
λ ₯μ λν΄μ μ¦μ μΊμλ κ° λ°ν
π§© μ 리: κ°μ‘°λ λΆλΆμ μλ―Έ
| λ²νΈ | μ½λ | μ€λͺ |
|---|---|---|
| 1οΈβ£ | Function.prototype.memo | λͺ¨λ ν¨μμ μΊμ κΈ°λ₯ λΆμ¬ |
| 2οΈβ£ | sayHello.memo(...) | λ¨μ λ¬Έμμ΄ λ°ν ν¨μλ μΊμ± κ°λ₯ |
| 3οΈβ£ | isPrime.memo(...) | κ³ λΉμ© μ°μ°μ μΊμ± ν¨κ³Όλ₯Ό 보μ¬μ£Όλ μ¬λ‘ |
index.html 컀λ§(currying) κΈ°λ² λΌμ΄λΈλ¬λ¦¬ μ€ν¬λ¦½νΈ λ±λ‘
ex02
ex03
ex04