๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive ๋์์ 22์ฅ์ ์ ๋ฆฌํ์์ต๋๋ค.
๋์์ ๋ํ๋ด๋ ๋ฉ์๋๋ ์์ ์ด ์ํ ๊ฐ์ฒด์ ์ํ, ์ฆ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ๋ณ๊ฒฝํ ์ ์์ด์ผ ํ๋ค. ๋ฉ์๋๊ฐ ์์ ์ด ์ํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ผ ํ๋ค.
// ์์ 22-01
const circle = {
// ํ๋กํผํฐ: ๊ฐ์ฒด ๊ณ ์ ์ ์ํ ๋ฐ์ดํฐ
radius: 5,
// ๋ฉ์๋: ์ํ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ์กฐ์ํ๋ ๋์
getDiameter() {
// ์ด ๋ฉ์๋๊ฐ ์์ ์ด ์ํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๋ ค๋ฉด ์์ ์ด ์ํ ๊ฐ์ฒด์ธ circle์ ์ฐธ์กฐํ ์ ์์ด์ผ ํ๋ค.
return 2 * circle.radius;
}
};
console.log(circle.getDiameter()); //10
this๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์๋ค. this๋ฅผ ํตํด ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ, ์ฆ this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ์ํด ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
// 22-03
const circle = {
radius: 5,
getDiameter() {
// this๋ circle์ ๊ฐ๋ฆฌํจ๋ค.
return 2 * this.radius;
}
};
// 22-05
console.log(this); //window
function square(number) {
console.log(this); // window
return number * number;
}
square(2);
const person = {
name: 'waterglasses',
getName() {
// ๋ฉ์๋ ๋ด๋ถ์ this: ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด
console.log(this); // {name: 'waterglasses", getName: f}
return this.name;
}
}
console.log(person.getName()); // waterglasses
'use strict'
function Person(name) {
this.name = name;
console.log(this);
}
function Person(name) {
this.name = name;
// ์์ฑ์ ํจ์ ๋ด๋ถ์ this: ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค
console.log(this); // Person {name: "waterglasses"}
}
const me = new Person('waterglasses');
this ๋ฐ์ธ๋ฉ์ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
// 22-07
function foo() {
console.log("foo's this: ", this); // window
}
foo();
// 22-11
var value = 1;
const obj = {
value: 100,
foo() {
const that = this; // this๋ฅผ that์ ํ ๋น
setTimeout(function () {
console.log(that.value);
}, 100);
// ํน์
setTimeout(() => console.log(this.valueu), 100); // 100
}
};
obj.foo();
๋ฉ์๋ ๋ด๋ถ์ this์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด, ์ฆ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋ฉ์๋ ์ด๋ฆ ์์ ๋ง์นจํ(.) ์ฐ์ฐ์ ์์ ๊ธฐ์ ํ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
// 22-16
function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
}
const me = new Person('Lee');
// me๊ฐ ํธ์ถ
console.log(me.getName()); // Lee
์์ฑ์ ํจ์ ๋ด๋ถ์ this์๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค์ ๋ฐ์ธ๋ฉ๋๋ค. new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ๋ฉด ์์ฑ์ ํจ์๋ก ๋์ํ๋ค.
// 22-18
const circle3 = Circle(15);
console.log(circle3); //undefined
console.log(radius); //15
Function.prototype.apply/call ๋ฉ์๋๋ this๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ใ ฃใ ใด์๋ก ์ ๋ฌ๋ฐ์ ํจ์๋ฅผ ํธ์ถํ๋ค.
// 22-19
function getThisBinding() {
return this;
}
const thisArgs = { a: 1 };
console.log(getThisBinding()); // window
console.log(getThisBinding.apply(thisArgs)); // {a: 1}window
console.log(getThisBinding.call(thisArgs)); // {a: 1}
console.log(getThisBinding.bind(thisArg)); // getThisBinding
console.log(getThisbinding.bind(thisArg)()); // {a: 1}
// 22-23
const person = {
name: 'Lee',
foo(callback) {
console.log(this); // {name: 'Lee', foo: ฦ}
setTimeout(callback, 100);
// bind๋ก ํด๊ฒฐํ ์ ์์
setTimeout(callback.bind(this), 100);
}
}
person.foo(function () {
console.log(this.name); // window์์๋ '', Node.js์์๋ undefined
})
Ref
- ์ด์ ๋ชจ ์ , โ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Diveโ, ์ํค๋ถ์ค