const circle ={
radius : 5,
getDiameter() {
return 2 * circle.radius;
}
};
console.log(circle.getDiameter()); // 10
- ์ ์์ ์ ๊ฒฝ์ฐ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ํ๊ฐ๊ฐ ์๋ฃ๋์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋์๊ณ , circle ์๋ณ์์ ์์ฑ๋ ๊ฐ์ฒด๊ฐ ํ ๋น๋ ์ดํ์ด๊ธฐ ๋๋ฌธ์ ๋ฉ์๋ ๋ด์์ circle ์๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
- ํ์ง๋ง ์ฌ๊ท์ ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๋ฐฉ์์ ์ผ๋ฐ์ ์ด์ง๋ ์๊ณ ๋ฐ๋์งํ์ง ์๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ์ ์ผ๋ฐ์ ์ธ ํจ์๋ค์ this๋ผ๋ ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- this๋ ํด๋น ํจ์๋ฅผ ๋ฉ์๋๋ก ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด ๋๋ ํด๋น ํจ์๊ฐ ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๋ณ์์ด๋ค.
- this๋ ์๋ฌต์ ์ผ๋ก ์์ฑ๋๊ณ , ์ฝ๋ ์ด๋์๋ ์ฐธ์กฐ ๊ฐ๋ฅํ๋ค.
- this๋ ํจ์ ๋ด์์ ์ฐ์ผ ๋ ์๋ฏธ๊ฐ ์์ง๋ง ์ ์ญ์์๋ ์ฐธ์กฐํ ์๋ ์๋ค.
- this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ, ์ฆ this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ์ํด ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
- this๋ฅผ ์ ์ญ์์ ์ฐธ์กฐํ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ๋์ํ๋ค.
- ๋ธ๋ผ์ฐ์ ์์ ๋์ํ ๊ฒฝ์ฐ this๋ ์ ์ญ ๊ฐ์ฒด์ธ window๋ฅผ ์ฐธ์กฐํ๋ค.
- node REPL์์ this๋ ์ ์ญ ๊ฐ์ฒด์ธ global์ ์ฐธ์กฐํ๋ค.
- commonJS์ ๋ชจ๋ ์์คํ ์ ๋ฐ๋ฅด๋ node ํ๊ฒฝ์์๋ module.exports๋ฅผ ์๋ฏธํ๋ค.
- ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์ผ ๋, strict mode๋ผ๋ฉด undefined๊ฐ ํ ๋น๋๋ค.
this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์, ์ฆ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
์ฃผ์ํ ๊ฒ์ ๋์ผํ ํจ์๋ ๋ค์ํ ๋ฐฉ์์ผ๋ก ํธ์ถํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก this์๋ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋๋ค.
- ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๋ ์ผ๋ฐํจ์์์์ this๋ ์๋ฏธ๊ฐ ์๋ค.
- strict mode๊ฐ ์ ์ฉ๋ ์ผ๋ฐํจ์ ๋ด๋ถ์ this์๋ undefined๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
function foo() {
console.log("foo's this: ", this); //window
function bar() {
console.log("bar's this: ", this); //window
}
bar();
}
foo();
- ์ฝ๋ฐฑ ํจ์๊ฐ ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋๋ค๋ฉด ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this์๋ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
- ํ์ง๋ง ๋ฉ์๋ ๋ด์์ ์ ์ํ ์ค์ฒฉํจ์ ๋๋ ๋ฉ์๋์๊ฒ ์ ๋ฌํ ์ฝ๋ฐฑํจ์๊ฐ ์ผ๋ฐํจ์๋ก ํธ์ถ๋ ๋ ๊ทธ ํจ์๋ค์ this๊ฐ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๊ฒ์ ๋ฌธ์ ๊ฐ ์๋ค.
- ์ธ๋ถ ํจ์์ธ ๋ฉ์๋์ ์ค์ฒฉํจ์ ๋๋ ์ฝ๋ฐฑ ํจ์์ this๊ฐ ์ผ์นํ์ง ์๋๋ค๋ ๊ฒ์ ์ค์ฒฉ, ์ฝ๋ฐฑ ํจ์๋ฅผ ํฌํผํจ์๋ก์ ๋์ํ๊ธฐ ์ด๋ ต๊ฒ ํ๋ค.
ํด๊ฒฐ์ฑ
- this ๋ฐ์ธ๋ฉ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋น
var value = 1;
const obj = {
value: 100,
foo() {
const that = this;
setTimeout(function () {
console.log(that.value);
}, 100);
},
};
obj.foo();
- ์ฝ๋ฐฑํจ์์ ๋ช ์์ ์ผ๋ก this๋ฅผ ๋ฐ์ธ๋ฉ
var value = 1;
const obj = {
value: 100,
foo() {
setTimeout(function () {
console.log(this.value);
}.bind(this), 100);
},
};
obj.foo();
- ํ์ดํ ํจ์ ์ด์ฉ
ํ์ดํ ํจ์ ๋ด๋ถ์ this๋ ์์ ์ค์ฝํ์ this๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
var value = 1;
const obj = {
value: 100,
foo() {
setTimeout(()=>console.log(this.value), 100);
}
};
obj.foo();
const person = {
name: 'Lee',
getName() {
return this.name;
}
};
console.log(person.getName()); //Lee
์ ์์ ์ getName ๋ฉ์๋๋ person ๊ฐ์ฒด์ ๋ฉ์๋๋ก ์ ์๋์๋ค.
- ๋ฉ์๋๋ ํ๋กํผํฐ์ ๋ฐ์ธ๋ฉ๋ ํจ์์ด๊ธฐ์ getNameํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ํจ์ ๊ฐ์ฒด๋ person์ ํฌํจ๋ ๊ฒ์ด ์๋, ๋ ๋ฆฝ์ ์ผ๋ก ์กด์ฌํ๋ ๋ณ๋์ ๊ฐ์ฒด์ด๋ค.
- ๋ฐ๋ผ์ ๋ฉ์๋ ๋ด๋ถ์ this๋ ํ๋กํผํฐ๋ก ๋ฉ์๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ์๊ด ์๊ณ , ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค.
- ํ๋กํ ํ์ ๋ฉ์๋ ๋ด๋ถ์์ ์ฌ์ฉ๋ this๋ ์ผ๋ฐ ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค.
-
new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋๋ ๊ฒฝ์ฐ ํจ์๋ ์์ฑ์๋ก์จ ํธ์ถ๋๋ค.
-
์์ฑ์ ํจ์ ๋ด๋ถ์์ this๋ ์์ฑํ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
-
์์ฑ์ ํจ์์ ๋์์ ๊ฐ๋จํ ๋ํ๋ด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
function Circle(radius) { this.radius = radius; this.getDiameter = function() { return 2*this.radius; }; } const circle1 = new Circle(5); const circle2 = new Circle(10); console.log(circle1.getDiameter()); //10 console.log(circle2.getDiameter()); //20 //new ์ฐ์ฐ์ ์์ด ํธ์ถ๋๋ฉด ์ผ๋ฐ์ ์ธ ํจ์ ํธ์ถ์ด๋ค const circle3 = Circle(15); console.log(circle3); //undefined //์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ Circle ๋ด๋ถ์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. console.log(radius); //15
apply, call,bind ๋ฉ์๋๋ Function.prototype์ ๋ฉ์๋์ด๋ค. ์ฆ ๋ชจ๋ ํจ์๊ฐ ์ด๋ฅผ ์์๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
-
apply, call
- apply์ call ๋ฉ์๋๋ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์๋ง ๋ค๋ฅผ ๋ฟ ๋์ผํ๊ฒ ๋์ํ๋ค.
- apply ๋ฉ์๋๋ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฌถ์ด ์ ๋ฌํ๋ค.
- call ๋ฉ์๋๋ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ๋ ๋ฆฌ์คํธ ํ์์ผ๋ก ์ ๋ฌํ๋ค.
- ๋ํ์ ์ธ ์ฉ๋๋ arguments ๊ฐ์ฒด์ ๊ฐ์ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ค.
- arguments ๊ฐ์ฒด๋ ๋ฐฐ์ด์ด ์๋๊ธฐ๋๋ฌธ์ slice ๋ฉ์๋๊ฐ์ ๋ฐฐ์ด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
function getThisBinding() { console.log(arguments); return this; } const thisArgs = { a: 1 }; //apply ๋ฉ์๋๋ ๋ฐฐ์ด ํ์ console.log(getThisBinding.apply(thisArgs, [1, 2, 3])); //call ๋ฉ์๋๋ ๋ฆฌ์คํธ ํ์ console.log(getThisBinding.call(thisArgs, 1, 2, 3));
-
bind
- bind๋ฉ์๋๋ apply, call ๋ฉ์๋์ ๋ฌ๋ฆฌ ํจ์๋ฅผ ํธ์ถํ์ง ์๋๋ค. ๋ค๋ง ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ๊ฐ์ผ๋ก this ๋ฐ์ธ๋ฉ์ด ๊ต์ฒด๋ ํจ์๋ฅผ ์๋กญ๊ฒ ์์ฑํด ๋ฐํํ๋ค.
function getThisBinding() { return this; } const thisArgs = { a: 1 }; console.log(getThisBinding.bind(thisArgs)); //getThisBinding //bind ๋ฉ์๋๋ ํจ์๋ฅผ ํธ์ถํ์ง ์์ผ๋ฏ๋ก ๋ช ์์ ์ผ๋ก ํธ์ถํด์ผํ๋ค. console.log(getThisBinding.bind(thisArgs)()); //{ a: 1 }
- bind ๋ฉ์๋๋ ๋ฉ์๋์ this์ ๋ฉ์๋ ๋ด๋ถ์ ์ค์ฒฉ ํจ์ ๋๋ ์ฝ๋ฐฑ ํจ์์ this๊ฐ ๋ถ์ผ์นํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ์ํด ์ฌ์ฉ๋๋ค.
const person = { name : 'Lee', foo(callback){ setTimeout(callback, 100); } }; person.foo(function(){ console.log(`Hi my name is ${this.name}.`); //Hi my name is . //๋ธ๋ผ์ฐ์ ํ๊ฒฝ ์์์ window.name์ ๋ธ๋ผ์ฐ์ ์ฐฝ์ ์ด๋ฆ์ ๋ํ๋ด๋ //๋นํธ์ธ ํ๋กํผํฐ์ด๋ฉฐ ๊ธฐ๋ณธ ๊ฐ์ ''์ด๋ค. })
const person = { name : 'Lee', foo(callback){ //bind ๋ฉ์๋๋ก ์ฝ๋ฐฑํจ์ ๋ด๋ถ์ this ๋ฐ์ธ๋ฉ์ ์ ๋ฌ. setTimeout(callback.bind(this), 100); } }; person.foo(function(){ console.log(`Hi my name is ${this.name}.`); //Hi my name is Lee. })