โ ์ค๋ ์๊ฐ ๋๋ ๊ทธํ ๋ก ๋น์ธ๊ณ , ๊ทธํ ๋ก ์ฒจ๋จ์ธ ๋ฌด์์ด ๊ทธํ ๋ก ์ธ๋ชจ ์์ ์ ์๋์ง ์ดํดํ์ง ๋ชปํ๋ค.
๊ทธ๋ฌ๋ค๊ฐ ์ปดํจํฐ๋ ๋๋๋๋ก ๋๋ํ ๊ฒ์ ํ ์ ์๋ ๋ฉ์ฒญํ ๊ธฐ๊ณ์ด๊ณ , ์ปดํจํฐ ํ๋ก๊ทธ๋๋จธ๋ ๋๋๋๋ก ๋ฉ์ฒญํ ์ง์ ํ ์ ์๋ ๋๋ํ ์ฌ๋์ด๋ผ๋ ์๊ฐ์ด ๋ค์๋ค.
์ฆ, ๋์ ์๋ฒฝํ ํ ์์ด๋ค. โ- Bill Bryson
์ ๋ช ํ ๋ฏธ๊ตญ์ ์๊ฐ

this ์ ์
let group = {
title: "1๋ชจ๋ ",
students: ["๋ณด๋ผ", "ํธ์ง", "์ง๋ฏผ"],
title2 : this.title,
title3() { console.log(this.title) }
};
console.log(group.title2); //undefined
group.title3(); // 1๋ชจ๋
this๋ ํจ์์ ๋ธ๋ก ์ค์ฝํ ๋ด์์ ์ ์ธ ๋์ผ ์๋ํ๋ค.
๋ธ๋ผ์ฐ์ ์ฝ์(F12)์ ์ผ๊ณ , this๋ฅผ ์ณ๋ณด์
this; // Window {}
์ด๋ฒ์ ๋ณ์์ ํจ์ ์์ ๋ฃ์ด์ ํด๋ณด์.
var ga = 'Global variable';
console.log(this.ga); // === window.ga
function a() { console.log(this); };
a(); // Window {}
window ์ด๋ค.
(ํจ์ ์ผ ๊ฒฝ์ฐ strict ๋ชจ๋์ผ ๊ฒฝ์ฐ๋ undefined).
์ฌ๊ธฐ์ ํ ๊ฐ์ง ์ฌ์ค์ ์ ์ ์๋ค.
โ
this๋ ๊ธฐ๋ณธ์ ์ผ๋ก window ์ด๋ค.
์ผ๋ฐ ํจ์ ๋ด์์ ํผ์ this๋ฅผ ์ ์ธํ๋ฉด, ๊ทธ this๋ window๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํจ๋ค.
โ
์ด๋ฒ์, ์ผ๋ฐ ํจ์๊ฐ ์๋ ๊ฐ์ฒด์ ๋ฉ์๋์ ๊ฒฝ์ฐ๋ฅผ ๋ณด์.
var obj = {
a: function() { console.log(this); },
};
obj.a(); // obj
๊ฐ์ฒด ๋ฉ์๋ a ์์ this๋ ๊ฐ์ฒด obj๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
์ด๊ฒ์ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ this๋ฅผ ๋ด๋ถ์ ์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ ๋๋ฌธ์ ๊ทธ๋ ๋ค.
โ
๋จ ์์ ์์ ์์ ๋ค์๊ณผ ๊ฐ์ด ํ๋ฉด ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง๋ค.
var a2 = obj.a;
a2(); // window
a2๋ obj.a๋ฅผ ๊บผ๋ด์จ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ ์ด์ obj์ ๋ฉ์๋๊ฐ ์๋๊ณ ๋ณ์์ ๋ด๊ธด ๊ทธ๋ฅ ์ผ๋ฐํจ์ ์ด๋ค.
ํธ์ถํ ๋,
ํธ์ถํ๋ ํจ์๊ฐ ๊ฐ์ฒด์ ๋ฉ์๋์ธ์ง ๊ทธ๋ฅ ํจ์์ธ์ง๊ฐ ์ค์ํ๋ค.
Java์์์ this๋ ์ธ์คํด์ค ์์ (self)์ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๋ณ์์ด๋ค.
this๊ฐ ๊ฐ์ฒด ์์ ์ ๋ํ ์ฐธ์กฐ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ค๋ ๋ป์ด๋ค.
์ฃผ๋ก ๋งค๊ฐ๋ณ์์ ๊ฐ์ฒด ์์ ์ด ๊ฐ์ง๊ณ ์๋ ๋ฉค๋ฒ๋ณ์๋ช ์ด ๊ฐ์ ๊ฒฝ์ฐ ์ด๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด์ ์ฌ์ฉ๋๋ค.
์๋ Java ์ฝ๋์ ์์ฑ์ ํจ์ ๋ด์ this.name์ ๋ฉค๋ฒ๋ณ์๋ฅผ ์๋ฏธํ๋ฉฐ name์ ์์ฑ์ ํจ์๊ฐ ์ ๋ฌ๋ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ์๋ฏธํ๋ค.
// JAVA
public Class Person {
private String name;
public Person(String name) {
this.name = name; // this.name๊ณผ ๊ทธ๋ฅ name์ ์์ ํ ๋ค๋ฅธ ๋์ด๋ค.
}
}
ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฒฝ์ฐ
Java์ ๊ฐ์ด this์ ๋ฐ์ธ๋ฉ๋๋ ๊ฐ์ฒด๋ ํ๊ฐ์ง๋ก ๊ณ ์ ๋๋๊ฒ ์๋๋ผ
ํด๋น ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ this์ ๋ฐ์ธ๋ฉ๋๋ ๊ฐ์ฒด๊ฐ ๋ฌ๋ผ์ง๋ค.
ํจ์ ํธ์ถ ๋ฐฉ์๊ณผ this ๋ฐ์ธ๋ฉ
์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฒฝ์ฐ ํจ์ ํธ์ถ ๋ฐฉ์์ ์ํด this์ ๋ฐ์ธ๋ฉํ ์ด๋ค ๊ฐ์ฒด๊ฐ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
๋ค์ ๋งํด, ํจ์๋ฅผ ์ ์ธํ ๋ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ์ ์ ์ผ๋ก ๊ฒฐ์ ๋๋ ๊ฒ์ด ์๋๊ณ , ํจ์๋ฅผ ํธ์ถํ ๋ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
โ
ํจ์์ ํธ์ถํ๋ ๋ฐฉ์์ ์๋์ ๊ฐ์ด ๋ค์ํ๋ค.
- ํจ์ ํธ์ถ
- ๋ฉ์๋ ํธ์ถ
- ์์ฑ์ ํจ์ ํธ์ถ
- ์ฝ๋ฐฑ ํธ์ถ
- apply/call/bind ํธ์ถ
โ
1. ํจ์ ํธ์ถ
โ
๊ธฐ๋ณธ์ ์ผ๋ก this๋ ์ ์ญ๊ฐ์ฒด(Global object)์ ๋ฐ์ธ๋ฉ๋๋ค.
์ผ๋ฐ ์ ์ญํจ์๋ ๋ฌผ๋ก ์ด๊ณ ๋ด๋ถํจ์์ ๊ฒฝ์ฐ๋ this๋ ์ธ๋ถํจ์๊ฐ ์๋ ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
function foo() {
console.log("foo's this: ", this); // window
function bar() {
console.log("bar's this: ", this); // window
}
bar();
}
foo();
โ
๊ฐ์ฒด์ ๋ฉ์๋์ ๋ด๋ถํจ์์ผ ๊ฒฝ์ฐ์๋ this๋ ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
var value = 1;
var obj = {
value: 100,
foo: function() {
console.log("foo's this: ", this); // obj
console.log("foo's this.value: ", this.value); // 100
function bar() { /* ๋ด๋ถํจ์ */
console.log("bar's this: ", this); // window
console.log("bar's this.value: ", this.value); // 1
}
bar();
}
};
obj.foo();
โ
์ฝ๋ฐฑํจ์์ ๊ฒฝ์ฐ์๋ this๋ ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
var value = 1;
var obj = {
value: 100,
foo: function() {
setTimeout(function() { /* ์ฝ๋ฐฑํจ์ */
console.log("callback's this: ", this); // window
console.log("callback's this.value: ", this.value); // 1
}, 100);
}
};
obj.foo();
๋ด๋ถํจ์๋ ์ผ๋ฐ ํจ์, ๋ฉ์๋, ์ฝ๋ฐฑํจ์ ์ด๋์์ ์ ์ธ๋์๋ ๊ด๊ณ์์ด this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉํ๋ค.
โ
๋ด๋ถํจ์์ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ ํํผ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
- var that = this; ๋ฅผ ํด์ ๊ฐ์ฒด์ this๋ฅผ ๋ณ์์ ์ ์ฅํด ์ฌ์ฉ
- call,bind,apply๋ก this ์ค์
- => ํ์ดํ ํจ์ ์ฌ์ฉ
var value = 1;
var obj = {
value: 100,
foo: function() {
var that = this; // Workaround : this === obj
console.log("foo's this: ", this); // obj
console.log("foo's this.value: ", this.value); // 100
function bar() {
//console.log("bar's this: ", this); // window
// console.log("bar's this.value: ", this.value); // 1
console.log("bar's that: ", that); // obj
console.log("bar's that.value: ", that.value); // 100
}
bar();
}
};
obj.foo();

โ
2. ๋ฉ์๋ ํธ์ถ
โํจ์๊ฐ ๊ฐ์ฒด์ ํ๋กํผํฐ ๊ฐ์ด๋ฉด ๋ฉ์๋๋ก์ ํธ์ถ๋๋ค.
์ด๋ ๋ฉ์๋ ๋ด๋ถ์ this๋ ํด๋น ๋ฉ์๋๋ฅผ ์์ ํ ๊ฐ์ฒด, ์ฆ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
var obj1 = {
name: 'Lee',
sayName: function() {
console.log(this.name);
}
}
var obj2 = {
name: 'Kim'
}
obj2.sayName = obj1.sayName;
obj1.sayName(); // Lee
obj2.sayName(); // Kim

โ
โ
3. ํ๋กํ ํ์
ํ๋กํ ํ์ ๊ฐ์ฒด๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์๋ค.
ํ๋กํ ํ์ ๊ฐ์ฒด ๋ฉ์๋ ๋ด๋ถ์์ ์ฌ์ฉ๋ this๋ ์ผ๋ฐ ๋ฉ์๋ ๋ฐฉ์๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ํ๋กํ ํ์ ์ค๋ธ์ ํธ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
๊ทธ๋์ this์ ํ๋กํผํฐ๋ฅผ ์ฐพ์ ๋ ์ฐ์ , ์ง์ ๋ฐ์ธ๋ฉ ๋์ด์๋ ํ๋กํ ํ์ ์ค๋ธ์ ํธ์์ ์ฐพ๊ณ , ์์ผ๋ฉด ์ฒด์ด๋์ ์ํด new์์ฑ์๋ก ์์ฑ๋ ๊ฐ์ฒด์์ ์ฐพ๊ฒ ๋๋๋ค.
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var me = new Person('Lee');
console.log(me.getName());
// Lee
// ์ฐ์ ํ๋กํ ํ์
์์ nameํ๋กํผํฐ๋ฅผ ์ฐพ๋๋ค. ์์ผ๋ ์ฒด์ด๋์ ์ํด me ๊ฐ์ฒด์์ ์ฐพ์์ ๋ฐํ
Person.prototype.name = 'Kim';
console.log(Person.prototype.getName());
// Kim
// ์ฐ์ ํ๋กํ ํ์
์์ nameํ๋กํผํฐ๋ฅผ ์ฐพ๋๋ค. ์ฐพ์์ผ๋ ๋ฐํ.

โ
4 ์์ฑ์ ํจ์ ํธ์ถ
์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฑ์ ํจ์๋ ๋ง ๊ทธ๋๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ญํ ์ ํ๋ค.
โ
ํ์ง๋ง ์๋ฐ์ ๊ฐ์ ๊ฐ์ฒด์งํฅ ์ธ์ด์ ์์ฑ์ ํจ์์๋ ๋ค๋ฅด๊ฒ ๊ทธ ํ์์ด ์ ํด์ ธ ์๋ ๊ฒ์ด ์๋๋ผ
๊ธฐ์กด ํจ์์ new ์ฐ์ฐ์๋ฅผ ๋ถ์ฌ์ ํธ์ถํ๋ฉด ํด๋น ํจ์๋ ์์ฑ์ ํจ์๋ก ๋์ํ๋ค.
โ
์ด๋ ๋ฐ๋๋ก ์๊ฐํ๋ฉด ์์ฑ์ ํจ์๊ฐ ์๋ ์ผ๋ฐ ํจ์์ new ์ฐ์ฐ์๋ฅผ ๋ถ์ฌ ํธ์ถํ๋ฉด ์์ฑ์ ํจ์์ฒ๋ผ ๋์ํ ์ ์๋ค.
๋ฐ๋ผ์ ์ผ๋ฐ์ ์ผ๋ก ์์ฑ์ ํจ์๋ช ์ ์ฒซ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๊ธฐ์ ํ์ฌ ํผ๋์ ๋ฐฉ์งํ๋ ค๋ ๋ ธ๋ ฅ์ ํ๋ค.
// ์์ฑ์ ํจ์
function Person(name) {
this.name = name;
}
var me = new Person('Lee');
console.log(me); // Person {name: "Lee"}
// new ์ฐ์ฐ์์ ํจ๊ป ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ์ง ์์ผ๋ฉด ์์ฑ์ ํจ์๋ก ๋์ํ์ง ์๋๋ค.
var you = Person('Kim');
console.log(you); // undefined
โ
5. ์ฝ๋ฐฑ ํจ์ ํธ์ถ
let userData = {
signUp: '2020-10-06 15:00:00',
id: 'minidoo',
name: 'Not Set',
setName: function(firstName, lastName) {
this.name = firstName + ' ' + lastName;
}
}
function getUserName(firstName, lastName, callback) {
callback(firstName, lastName);
}
getUserName('PARK', 'MINIDDO', userData.setName);
console.log('1: ', userData.name); // Not Set
console.log('2: ', window.name); // PARK MINIDDO
์ฐ๋ฆฌ๋ ์ฒซ ๋ฒ์งธ ์ฝ์์ ๊ฐ์ด PAKR MINIDDO ์ด๊ธฐ๋ฅผ ๊ธฐ๋ํ์ง๋ง, Not Set์ด ์ถ๋ ฅ๋๋ค.
setName() ํจ์๊ฐ ์คํ๋๊ธฐ ์ ์ name ๊ฐ์ด ๋์ค๋ ๊ฒ์ธ๋ฐ, ์ด๋ getUserName() ์ด ์ ์ญ ํจ์์ด๊ธฐ ๋๋ฌธ์ด๋ค.
userData.setName๋ฅผ ์๊ท๋จผํธ๋ก ๋๊ฒจ์ค๋ CALL BY VALUE๋ก ๊ฐ๋๊ฑธ ๋ช ์ฌํด์ผ ํ๋ค. (JS๋ ๋ฌด์กฐ๊ฑด call by value)
ํ๋ง๋๋ก ํจ์๊ฐ ๋ณต์ฌ๋์ด callback ํ๋ผ๋ฏธํฐ์ ๋ด๊ธฐ๊ฒ ๋๋, ๋น์ฐํ setName()์ this๋ ์ ์ญ๊ฐ์ฒด window๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ ๊ฒ์ด๋ค.
ํด๊ฒฐ ๋ฐฉ์ : call()๊ณผ apply()๋ฅผ ์ฌ์ฉํ์ฌ this๋ฅผ ๋ณดํธํ ์ ์๋ค.
function getUserName(firstName, lastName, callback) {
callback.call(userData, firstName, lastName);
}
getUserName('PARK', 'MINIDDO', userData.setName);
console.log('1: ', userData.name); // PARK MINIDDO
console.log('2: ', window.name); // ๋น์นธ. ์๋ํ๋ฉด ๋ณ์๊ฐ ์์ผ๋๊น
โ
6. apply/call/bind ํธ์ถ
func.apply(thisArg, [argsArray])
func.call(thisArg, argsArray)
func.bind(thisArg)(argsArray)
// thisArg: ํจ์ ๋ด๋ถ์ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด
// argsArray: ํจ์์ ์ ๋ฌํ argument์ ๋ฐฐ์ด
๊ธฐ์ตํด์ผ ํ ๊ฒ์ apply() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ฃผ์ฒด๋ funcํจ์์ด๋ฉฐ .apply() ๋ฉ์๋๋ this๋ฅผ ํน์ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉํ ๋ฟ ๋ณธ์ง์ ์ธ ๊ธฐ๋ฅ์ ํจ์ ํธ์ถ์ด๋ผ๋ ๊ฒ์ด๋ค.
โ
var name = "window name";
function Person(name) {
this.name = name;
}
Person.prototype.doSomething = function(callback) {
if(typeof callback == 'function') {
// --------- 1
callback();
}
};
function foo() {
console.log(this.name); // --------- 2
}
var p = new Person('Lee');
p.doSomething(foo); // window name
1์ ์์ ์์ this๋ Person ๊ฐ์ฒด์ด๋ค.
๊ทธ๋ฌ๋ 2์ ์์ ์์ this๋ ์ ์ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ์ฝ๋ฐฑํจ์ ๋ด๋ถ์ this๋ window๋ฅผ ๊ฐ๋ฆฌํจ๋ค!
โ
๋ฐ๋ผ์ ์ฝ๋ฐฑํจ์ ๋ด๋ถ์ this๋ฅผ ์ฝ๋ฐฑํจ์๋ฅผ, ํธ์ถํ๋ ํจ์ ๋ด๋ถ์ this์ ์ผ์น์์ผ ์ฃผ์ด์ผ ํ๋ค.
// --------- 1
callback.bind(this)();
์ค๋ฌด์์, ์ด๋ฒคํธ๋ฆฌ์ค๋๋ ์ ์ด์ฟผ๋ฆฌ๊ฐ์ ๊ฒ์ ์ผ์ ๋๋ฅผ ๋ณด์..
document.body.onclick = function() {
console.log(this); // <body>
}
์ด๊ฑด ๊ทธ๋ฅ ํจ์์ธ๋ฐ this๊ฐ window๊ฐ ์๋๋ผ <body>์ด๋ค.
๊ฐ์ฒด ๋ฉ์๋๋ ์๋๊ณ , bindํ ๊ฒ๋ ์๋๊ณ , new ๋ถ์ธ ๊ฒ๋ ์๋๋ฐ ๋ง์ด๋ค. ๋๊ฐ ๋ฐ๊ฟจ์๊น?
โ
๋ฐ๋ก ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋, ๋ด๋ถ์ ์ผ๋ก this๊ฐ ๋ฐ๋ ๊ฒ์ด๋ค.
๋ด๋ถ์ ์ผ๋ก ๋ฐ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋์์ ์ธ์ธ ์๋ฐ์ ์๋ค.
$('div').on('click', function() {
console.log(this);
});
์ด๋ฐ ์ ์ด์ฟผ๋ฆฌ ์ฝ๋ ๋ง์ด ๋ณธ์ ์ด ์์ ๊ฒ์ด๋ค. this๋ ํด๋ฆญํ div๊ฐ ๋๋ค.
๋ด๋ถ์ ์ผ๋ก function์ ํธ์ถํ ๋ ๊ทธ๋ ๊ฒ this๋ฅผ ๋ฐ๊ฟ๋ฒ๋ ธ๋ค. ์ด๋ฐ ๊ฑด ์ด์ฉ ์ ์์ด ์ธ์์ผ ํ๋ค.
$('div').on('click', function() {
console.log(this); // <div>
function inner() {
console.log('inner', this); // inner Window
}
inner();
});
์์ฉ์ฌ๋ก ๋ค.
๋ฐฉ๊ธ ์ ํด๋ฆญ ์ด๋ฒคํธ์์ ์ ์ด์ฟผ๋ฆฌ๊ฐ ๋ด๋ถ์ ์ผ๋ก this๋ฅผ ๋ฐ๊ฟ๋ฒ๋ฆฐ๋ค๊ณ ํ๋ค.
๊ทผ๋ฐ inner ํจ์ ํธ์ถ ์์๋ this๊ฐ window์ด๋ค. (์ผ๋ฐ์ ์ผ๋ก ๋ด๋ถํจ์์ this๋ window !!)
โ
๊ทธ์ click ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์๋ชปํ ๊ฒ์ด๋ค(์๋ชปํ๋ค๊ธฐ ๋ณด๋ค๋ ๋ด๋ถ์ ์ผ๋ก this๋ฅผ ๋ฐ๊ฟจ์์๋ ๋ช ์์ ์ผ๋ก ์๋ฆฌ์ง ์์ ๊ฒ).
โ
์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋
$('div').on('click', function() {
console.log(this); // <div>
var that = this; // <-------------------------------------------------------------
function inner() {
console.log('inner', that); // inner <div>
}
inner();
});
์์ฒ๋ผ this๋ฅผ that์ด๋ผ๋ ๋ณ์์ ์ ์ฅํ๋ ์ง
$('div').on('click', function() {
console.log(this); // <div>
const inner = () => {
console.log('inner', this); // inner <div>
}
inner();
});
ES6 ํ์ดํ ํจ์๋ฅผ ์ด๋ค.
ES6 ํ์ดํ ํจ์๋ this๋ก window ๋์ ์์ ํจ์์ this๋ฅผ ๊ฐ์ ธ์จ๋ค(์ฌ๊ธฐ์๋ <div>)
โ
์ฒด์ด๋
ํจ์์์ ์๊ธฐ ์์ this์ ๋ฆฌํดํ๋ฉด ์๊ธฐ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ธฐ ๋๋ฌธ์ ์ฐ์์ผ๋ก . ์ ์ฌ์ฉํ ์ ์๋ค.
let ladder = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this;
},
showStep() {
alert( this.step );
}
}
ladder.up().up().down().up().down().showStep(); // 1
์ ๋ฆฌํ์๋ฉด,
this๋ ๊ธฐ๋ณธ์ ์ผ๋ก window์ด์ง๋ง,
๊ฐ์ฒด ๋ฉ์๋, bind call apply, new์ผ ๋ this๊ฐ ๋ฐ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฒคํธ๋ฆฌ์ค๋๋ ๊ธฐํ document๋ผ์ด๋ธ๋ฌ๋ฆฌ์ฒ๋ผ this๋ฅผ ๋ด๋ถ์ ์ผ๋ก ๋ฐ๊ฟ ์๋ ์์ผ๋ํญ์ this๋ฅผ ํ์ธํด๋ด์ผ ํ๋ค.
โ
์ฌ๋ฌ๋ถ์ด ์ ์ธํ function์ this๋ ํญ์ window๋ผ๋ ๊ฒ ์์๋์.
(strict ๋ชจ๋์์๋ undefined !!)
โ
this ๊ฐ์ ๋ฐํ์์ ๊ฒฐ์ ๋๋ค
ํจ์๋ฅผ ์ ์ธํ ๋ this๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋ค๋ง, ํจ์๊ฐ ํธ์ถ๋๊ธฐ ์ ๊น์ง this์ ๊ฐ์ด ํ ๋น๋์ง ์๋๋ค.
โ
ํจ์๋ฅผ ๋ณต์ฌํด ๊ฐ์ฒด ๊ฐ ์ ๋ฌํ ์ ์๋ค.
ํจ์๋ฅผ ๊ฐ์ฒด ํ๋กํผํฐ์ ์ ์ฅํด object.method()๊ฐ์ด โ๋ฉ์๋โ ํํ๋ก ํธ์ถํ๋ฉด this๋ object๋ฅผ ์ฐธ์กฐํ๋ค.
โ
ํ์ดํ ํจ์๋ ์์ ๋ง์ this๋ฅผ ๊ฐ์ง์ง ์๋๋ค๋ ์ ์์ ๋ ํนํ๋ค.
ํ์ดํ ํจ์ ์์์ this๋ฅผ ์ฌ์ฉํ๋ฉด, ์ธ๋ถ์์ this ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
Refernece
https://www.zerocho.com/category/JavaScript/post/5b0645cc7e3e36001bf676eb
์ด ๊ธ์ด ์ข์ผ์ จ๋ค๋ฉด ๊ตฌ๋ & ์ข์์
์ฌ๋ฌ๋ถ์ ๊ตฌ๋
๊ณผ ์ข์์๋
์ ์์๊ฒ ํฐ ํ์ด ๋ฉ๋๋ค.