this
λ?λ€λ₯Έ λλΆλΆμ κ°μ²΄μ§ν₯ μΈμ΄μμμ this
μλ°μ€ν¬λ¦½νΈμμμ this
μ΄λμλ μ¬μ©ν μ μκ³ , μν©μ λ°λΌ this
κ° λ°λΌλ³΄λ λμμ΄ λ¬λΌμ§λ€.
β this
μ μλ λ°©μμ μ΄ν΄νμ§ λͺ»νλ©΄ λ¬Έμ κ° λ°μνμ λ μμΈμ μ°Ύμ ν΄κ²°νκΈ° μ΄λ ΅λ€.
μλ°μ€ν¬λ¦½νΈμμλ ν¨μμ κ°μ²΄(λ©μλ)μ ꡬλΆμ΄ λμ¨νλ€.
β this
κ° μ΄ λμ ꡬλΆνλ μ μΌν κΈ°λ₯
this
μλ°μ€ν¬λ¦½νΈμμμ this
λ μ€ν 컨ν
μ€νΈκ° μμ±λ λ κ²°μ
this
κ° κ²°μ λλ€. β ν¨μλ₯Ό μ΄λ€ λ°©μμΌλ‘ νΈμΆνλλμ λ°λΌ this
κ°μ΄ λ¬λΌμ§λ€.
this
β μ μκ°μ²΄!window
global
μμ 3-1, 3-2) μ μ 곡κ°μμμ this
// λΈλΌμ°μ νκ²½
console.log(this); // Window {0: global, window: Window, self: Window, document: document, name: '', location: Location, β¦}
console.log(window); // Window {0: global, window: Window, self: Window, document: document, name: '', location: Location, β¦}
console.log(this === window); // true
// Node.js νκ²½
console.log(this); // Object [global] {...}
console.log(global); // Object [global] {...}
console.log(this === window); // true
this
β μλ°μ€ν¬λ¦½νΈλ μν©λ³λ‘ this
ν€μλμ λ€λ₯Έ κ°μ λΆμ¬νκ² ν¨μΌλ‘μ¨ μ΄λ₯Ό ꡬν
"λ©μλλ κ°μ²΄μ νλ‘νΌν°μ ν λΉλ ν¨μμΌκΉμ?"
= λ°λ§λ°ν!
μμ 3-6) ν¨μλ‘μ νΈμΆ, λ©μλλ‘μ νΈμΆ
// λ³μ funcμ μ΅λͺ
ν¨μ ν λΉ
var func = function (x) {
console.log(this, x);
};
func(1); // Window { ... } 1 μ μκ°μ²΄ μΆλ ₯
// λ³μ objμ κ°μ²΄ ν λΉ
var obj = {
// κ°μ²΄μ method νλ‘νΌν°μ func ν¨μ ν λΉ
method: func
};
obj.method(2); // {method: Ζ} 2
β λ³μμ λ΄μ νΈμΆν κ²½μ°μ κ°μ²΄μ νλ‘νΌν°μ ν λΉν΄μ νΈμΆν κ²½μ° this
κ° λ¬λΌμ§λ€.
'ν¨μλ‘μ νΈμΆ'κ³Ό 'λ©μλλ‘μ νΈμΆ'μ μ΄λ»κ² ꡬλΆν κΉ?
μμ 3-7) λ©μλλ‘μ νΈμΆ - μ νκΈ°λ², λκ΄νΈ νκΈ°λ²
var obj = {
method: function (x) { console.log(this, x); }
};
obj.method(1); // {method: Ζ} 1
obj.['method'](2); // {method: Ζ} 2
this
this
μλ νΈμΆν 주체μ λν μ λ³΄κ° λ΄κΈ΄λ€.
this
μμ 3-8) λ©μλ λ΄λΆμμμ this
var obj = {
methodA: function (x) { console.log(this); },
inner: {
methodB: function (x) { console.log(this); },
}
};
obj.methodA(); // {inner: {β¦}, methodA: Ζ} ( === obj )
obj.inner.methodB(); // {methodB: Ζ} ( === obj.inner )
this
this
this
λ μ§μ λμ§ μλλ€.this
κ° μ§μ λμ§ μμ κ²½μ°, this
λ μ μ κ°μ²΄λ₯Ό λ°λΌλ³Έλ€.this
λ μ μ κ°μ²΄λ₯Ό λ°λΌλ³Έλ€.this
this
κ° λ¬΄μμ κ°λ¦¬ν€λμ§ μκ³ μλ€.this
μ κ°μ μ μ μλ€.μμ 3-9) λ΄λΆν¨μμμμ this
// obj1 κ°μ²΄ μμ±
var obj1 = {
// obj1 κ°μ²΄ λ΄λΆμ outer νλ‘νΌν°μ μ΅λͺ
ν¨μ μ°κ²°
outer: function () {
console.log('outer', this); // obj1 κ°μ²΄ μ λ³΄κ° μΆλ ₯
// innerFuncλ outer μ€μ½νμμλ§ μ κ·Όν μ μλ μ§μλ³μ
var innerFunc = function() {
console.log('innerFunc', this); // Window κ°μ²΄ μ λ³΄κ° μΆλ ₯
}
// innerFunc νΈμΆ
innerFunc();
// obj2λ outer μ€μ½νμμλ§ μ κ·Όν μ μλ μ§μλ³μ
// λ€μ κ°μ²΄ ν λΉ
var obj2 = {
// obj2 κ°μ²΄ λ΄λΆμ innerMethod νλ‘νΌν°μ innerFuncμ μ°κ²°λ μ΅λͺ
ν¨μ μ°κ²°
innerMethod: innerFunc
};
// obj2.innerMethod νΈμΆ
obj2.innerMethod(); // obj2 κ°μ²΄ μ λ³΄κ° μΆλ ₯
}
};
// obj1.outer νΈμΆ
obj1.outer();
obj1.outer
νΈμΆ
this
λ°μΈλ©this
μλ λ§μ§λ§ μ μμ κ°μ²΄μΈ obj1μ΄ λ°μΈλ©innerFunc
νΈμΆ
this
λ°μΈλ© this
κ° μ§μ λμ§ μμμΌλ―λ‘ μλμΌλ‘ μ€μ½ν 체μΈμμ μ΅μμ κ°μ²΄μΈ μ μκ°μ²΄(window) λ°μΈλ© obj2.innerMethod
νΈμΆ
this
μλ λ§μ§λ§ μ μμ κ°μ²΄μΈ obj2κ° λ°μΈλ©innerFunc
νΈμΆκ³Ό obj2.innerMethod
νΈμΆ
this
μ λμμ΄ μλ‘ λ€λ₯΄λ€.β this
λ°μΈλ©μ ν¨μλ₯Ό μ€ννλ λΉμμ μ£Όλ³ νκ²½(λ©μλ λ΄λΆμΈμ§, ν¨μ λ΄λΆμΈμ§ λ±)μ μ€μνμ§ μκ³ , ν΄λΉ ν¨μλ₯Ό νΈμΆνλ ꡬ문 μμ μ λλ λκ΄νΈ νκΈ°μ μ¬λΆκ° μ€μ
μμ 3-9 μΆλ ₯ κ²°κ³Ό)
νΈμΆ μ£Όμ²΄κ° μμ λλ μλμΌλ‘ μ μκ°μ²΄λ₯Ό λ°μΈλ©νμ§μκ³ , νΈμΆ λΉμ μ£Όλ³ νκ²½μ
this
λ₯Ό κ·Έλλ‘ μμλ°μ μ¬μ©ν μ μλ€λ©΄?
- λ³μλ₯Ό κ²μνλ©΄ μ°μ κ°μ₯ κ°κΉμ΄ μ€μ½νμ L.Eλ₯Ό μ°Ύκ³ , μλ€λ©΄ μμ μ€μ½νλ₯Ό νμνλ€.
this
λ νμ¬ μ»¨ν μ€νΈμ λ°μΈλ©λ λμμ΄ μμΌλ©΄ μ§μ 컨ν μ€νΈμthis
λ₯Ό λ°λΌλ³΄λλ‘ ν μ μμκΉ?β ES5κΉμ§λ μ체μ μΌλ‘ λ΄λΆν¨μμ
this
λ₯Ό μμν λ°©λ²μ μμΌλ μ°ννλ λ°©λ²μ μλ€.
this
λ₯Ό μ°ννλ λ°©λ²μμ 3-10) λ΄λΆν¨μμμμ this
λ₯Ό μ°ννλ λ°©λ² - λ³μ νμ©
this
λ₯Ό μ μ₯ν΄μ λ΄λΆν¨μμμ νμ©var obj = {
outer: function () {
console.log('outer', this); // obj κ°μ²΄ μ λ³΄κ° μΆλ ₯
var innerFunc1 = function() {
console.log('innerFunc1', this); // Window κ°μ²΄ μ λ³΄κ° μΆλ ₯
}
innerFunc1();
// outer μ€μ½νμμ selfλΌλ λ³μμ thisλ₯Ό μ μ₯
var self = this;
var innerFunc2 = function() {
console.log('innerFunc2', self); // obj κ°μ²΄ μ λ³΄κ° μΆλ ₯
}
innerFunc2();
}
};
obj.outer();
μμ 3-10 μΆλ ₯ κ²°κ³Ό)
this
λ₯Ό λ°μΈλ©νμ§ μλ ν¨μthis
κ° μ μκ°μ²΄λ₯Ό λ°λΌλ³΄λ λ¬Έμ λ₯Ό 보μνκΈ° μν΄, ES6μμλ this
λ₯Ό λ°μΈλ©νμ§ μλ νμ΄ν ν¨μλ₯Ό λμ
this
λ°μΈλ© κ³Όμ μ΄ λΉ μ§this
λ₯Ό κ·Έλλ‘ νμ© κ°λ₯this
μ°νλ²μ΄ λΆνμνλ€.μμ 3-11) this
λ₯Ό λ°μΈλ©νμ§ μλ ν¨μ(νμ΄ν ν¨μ)
var obj = {
outer: function () {
console.log(this); // {outer: Ζ}
var innerFunc = () => {
console.log(this); // {outer: Ζ}
};
innerFunc();
}
};
obj.outer();
μμ 3-11 μΆλ ₯ κ²°κ³Ό)
this
this
μμ ν¨μ B λ΄λΆ λ‘μ§μμ μ ν κ·μΉμ λ°λΌ κ°μ΄ κ²°μ this
κ° μ μκ°μ²΄λ₯Ό μ°Έμ‘°νμ§λ§, μ μ΄κΆμ λ°μ ν¨μμμ μ½λ°± ν¨μμ λ³λλ‘ this
κ° λ λμμ μ§μ ν κ²½μ° κ·Έ λμμ μ°Έμ‘°μ½λ°± ν¨μμμμ
this
μ μμ λ μ½λ°± ν¨μ: μ μ΄κΆ -this
μμ 4-6μ μ λ¦¬ν΄ λμμ΅λλ€.
this
λ₯Ό 무μμΌλ‘ ν μ§ κ²°μ this
this
λ 곧 μλ‘ λ§λ€ ꡬ체μ μΈ μΈμ€ν΄μ€ μμ μ΄ λλ€.μμ 3-13) μμ±μ ν¨μ
// λ³μ Catμ μ΅λͺ
ν¨μ ν λΉ
var Cat = function (name, age) {
// ν¨μ λ΄λΆμμ thisλ‘ μ κ·Ό
// bark, name, age νλ‘νΌν°μ κ°κ° κ°μ λμ
this.bark = 'μΌμΉ';
this.name = name;
this.age = age;
};
var choco = new Cat('μ΄μ½', 7);
var nabi = new Cat('λλΉ', 5);
console.log(choco, nabi); // κ°κ° Cat ν΄λμ€μ μΈμ€ν΄μ€ κ°μ²΄κ° μΆλ ₯
μμ 3-13 μΆλ ₯ κ²°κ³Ό)
this
λ₯Ό λ°μΈλ©νλ λ°©λ²this
μ λ°μΈλ© λλ κ°μ κ·μΉμ΄ μ‘΄μ¬this
μ λ³λμ λμμ λ°μΈλ© νλ λ°©λ²λ μ‘΄μ¬Function.prototype.call(thisArg[, arg1[, arg2[, ...]]])
μμμ κ°μ²΄λ₯Ό this
λ‘ μ§μ κ°λ₯
this
μ λ°μΈλ©μμ 3-14) call λ©μλ
// ν¨μ.call λ©μλ
var func = function (a, b, c) {
console.log('func: ', this, a, b, c)
}
func(1, 2, 3); // func: Window { ... } 1 2 3
func.call({ x:1 }, 4, 5, 6); // func: {x: 1} 4 5 6
// κ°μ²΄μ λ©μλ.call λ©μλ
// λ©μλμ λν΄μλ μμμ κ°μ²΄λ₯Ό thisλ‘ μ§μ κ°λ₯
var obj = {
a: 1,
method: function (x, y) {
console.log('obj.method: ', this.a, x, y);
}
}
obj.method(2, 3); // obj.method: 1 2 3
obj.method.call({ a: 4 }, 5, 6); // obj.method: 4 5 6
μμ 3-14 μΆλ ₯ κ²°κ³Ό)
Function.prototype.apply(thisArg[, argsArray])
μμ 3-16) apply λ©μλ
// ν¨μ.apply λ©μλ
var func = function (a, b, c) {
console.log('func: ', this, a, b, c)
}
func.apply({ x:1 }, [4, 5, 6]); // func: {x: 1} 4 5 6
// κ°μ²΄μ λ©μλ.apply λ©μλ
var obj = {
a: 1,
method: function (x, y) {
console.log('obj.method: ', this.a, x, y);
}
}
obj.method.apply({ a: 4 }, [5, 6]); // obj.method: 4 5 6
μμ 3-16 μΆλ ₯ κ²°κ³Ό)
Array.from
λ©μλλ₯Ό λμ
μμ 3-23) call / apply λ©μλμ νμ© - Math.max
/Max.min
μ apply μ μ©
var numbers = [10, 20, 3, 16, 45];
var max = Math.max.apply(null, numbers);
var min = Math.min.apply(null, numbers);
console.log(max, min); // 45 3
β call / apply λ©μλλ λͺ
μμ μΌλ‘ λ³λμ this
λ₯Ό λ°μΈλ©νλ©΄μ ν¨μ λλ λ©μλλ₯Ό μ€ννλ μ’μ λ°©λ²μ΄μ§λ§ μ€νλ € this
λ₯Ό μμΈ‘νκΈ° μ΄λ €μ
(ES5 μ΄νμ νκ²½μμλ λ€λ₯Έ λμμ΄ μμΌλ―λ‘ μ€λ¬΄μμ κ΄λ²μνκ² νμ©)
Function.prototype.bind(thisArg[, arg1[, arg2[, ...]]])
this
λ° μΈμλ€μ λ°νμΌλ‘ μλ‘μ΄ ν¨μλ₯Ό λ°νthis
λ₯Ό 미리 μ μ©νλ κ²μμ 3-25) bind λ©μλ - this
μ§μ κ³Ό λΆλΆ μ μ© ν¨μ ꡬν
var func = function (a, b, c, d) {
console.log(this, a, b, c, d);
};
func(1, 2, 3, 4); // Window{ ... } 1 2 3 4
// λ³μ bindFunc1μ funcμ thisλ₯Ό { x: 1 }λ‘ μ§μ ν μλ‘μ΄ ν¨μλ₯Ό μ μΈ
var bindFunc1 = func.bind({ x:1 });
bindFunc1(5, 6, 7, 8); // {x: 1} 5 6 7 8
// λ³μ bindFunc2μ funcμ thisλ₯Ό { x: 1 }λ‘ μ§μ
// μμμλΆν° λ κ°μ μΈμλ‘ κ°κ° 4, 5λ‘ μ§μ ν μλ‘μ΄ ν¨μ μ μΈ
var bindFunc2 = func.bind({ x:1 }, 4, 5);
bindFunc2(6, 7); // {x: 1} 4 5 6 7
bindFunc2(8, 9); // {x: 1} 4 5 8 9
bindFunc1
μ bind: this
λ§ μ§μ bindFunc2
μ bind: this
μ§μ κ³Ό λΆλΆ μ μ© ν¨μ ꡬνμμ 3-26) name νλ‘νΌν°
var func = function (a, b, c, d) {
console.log(this, a, b, c, d);
};
var bindFunc = func.bind({ x:1 }, 4, 5);
console.log(func.name); // func
console.log(bindFunc.name); // bound func
μμ 3-26 μΆλ ₯ κ²°κ³Ό)
this
λ₯Ό λ΄λΆν¨μλ μ½λ°± ν¨μμ μ λ¬νκΈ°μμ 3-27) λ΄λΆν¨μμ this
μ λ¬ - call, bind
// call λ©μλ
var obj = {
outer: function () {
console.log(this);
var innerFunc = function () {
console.log(this);
};
innerFunc.call(this);
}
};
obj.outer();
// bind λ©μλ
var obj = {
outer: function () {
console.log(this);
var innerFunc = function () {
console.log(this);
}.bind(this);
innerFunc();
}
};
obj.outer();
this
λ₯Ό λ°μΈλ©νλ κ³Όμ μ΄ μ μΈthis
κ° μμΌλ©°, μ κ·Όνκ³ μ νλ©΄ μ€μ½ν체μΈμ κ°μ₯ κ°κΉμ΄ this
μ μ κ·Όμμ 3-29) νμ΄ν ν¨μ λ΄λΆμμμ this
this
λ₯Ό μ°ννκ±°λ call, apply, bindλ₯Ό μ μ©ν νμκ° μλ€.var obj = {
outer: function () {
console.log(this);
var innerFunc = () => {
console.log(this);
};
innerFunc();
}
};
obj.outer();
μμ 3-29 μΆλ ₯ κ²°κ³Ό)
this
λ₯Ό λ°λ κ²½μ° (μ½λ°± ν¨μ λ΄μμμ this
)this
λ‘ μ§μ ν κ°μ²΄(thisArg)λ₯Ό μΈμλ‘ μ§μ ν μ μλ κ²½μ°κ° μλ€.this
κ°μ μνλ λλ‘ λ³κ²½ κ°λ₯μμ 3-31) μ½λ°± ν¨μμ ν¨κ» thisArgλ₯Ό μΈμλ‘ λ°λ λ©μλ
Array.prototype.forEach(callback[, thisArg])
Array.prototype.map(callback[, thisArg])
Array.prototype.filter(callback[, thisArg])
Array.prototype.some(callback[, thisArg])
Array.prototype.every(callback[, thisArg])
Array.prototype.find(callback[, thisArg])
Array.prototype.findIndex(callback[, thisArg])
Array.prototype.flatMap(callback[, thisArg])
Array.prototype.from(arrayLike[, callback[, thisArg]])
Set.prototype.forEach(callback[, thisArg])
Map.prototype.forEach(callback[, thisArg])
this
λ μ μκ°μ²΄λ₯Ό μ°Έμ‘°this
λ λ©μλ νΈμΆ 주체(λ©μλ λͺ
μμ κ°μ²΄)λ₯Ό μ°Έμ‘°this
λ μ μκ°μ²΄ μ°Έμ‘° (λ©μλμ λ΄λΆν¨μμμλ λμΌ)this
λ ν΄λΉ μ½λ°± ν¨μμ μ μ΄κΆμ λ겨λ°μ ν¨μκ° μ μν λ°μ λ°λ₯΄λ©°, μ μνμ§ μμ κ²½μ° μ μκ°μ²΄ μ°Έμ‘°this
λ μμ±λ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°this
λ°μΈλ©this
λ₯Ό λͺ
μμ μΌλ‘ μ§μ νλ©΄μ ν¨μ λλ λ©μλλ₯Ό νΈμΆthis
λ° ν¨μμ λκΈΈ μΈμλ₯Ό μΌλΆ μ§μ ν΄μ μλ‘μ΄ ν¨μλ₯Ό λ§λ¦this
λ₯Ό λ°κΈ°λ νλ€.μ°Έκ³
- μ½μ΄ μλ°μ€ν¬λ¦½νΈ - 03_this
this
μ λν λ°νλ₯Ό λ£κ² λμλ€.this
μ λν΄ λ¨Όμ κΈμ μ¬λ €μΌκ² λ€ νκ³ λ΄μ© μ 리λ₯Ό μμνλλ° μκ°λ³΄λ€ λ΄μ©μ΄ λ§μμ μκ°μ΄ μ€λκ±Έλ Έλ€.