this
this๋ ํจ์ ๋ด์์ ํธ์ถ ๋งฅ๋ฝ(context, ๊ฐ๋ณ์ ์ด๋ค)์ ์๋ฏธํ๋ค. this๋ ํจ์ ์์์๋ง ์ฌ์ฉ๊ฐ๋ฅํ๋ฉฐ ์ฝ์๋ ๋ณ์์ด๋ค.
ํจ์๊ฐ ์ด๋์ ์์๋์ด ์๋์ง๊ฐ ์ค์ํ๋ค
ํจ์์์์ this์ ์๋ฏธ
์ฒซ๋ฒ์งธ์์
function func(){
if(window === this){
return "window === this";
}
}
console.log(func());
์ฌ๊ธฐ์ this ๋ ์๋์ฐ๋ค
๋ฉ์๋์ ํธ์ถ
๋๋ฒ์งธ์์
var o = {
func: function(){
if(o === this){
return "o === this";
}
}
}
console.log(o.func());
o.func()์ this๋ o๋ฅผ ๊ฐ๋ฅดํจ๋ค.
์ ์ญ๊ฐ์ฒด, ์ ์ญ๋ฉ์๋๋ window๊ฐ ์๋ต๋์ด ์๋ค. ์ฒซ๋ฒ์งธ ์์์ this๋๋ฉ์๋๊ฐ ์์๋์ด ์๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ๋๋ฒ์งธ ์์๋ o์ ์์์ด๊ธฐ ๋๋ฌธ์ o๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๊ทธ๋์ ํจ์์ ํธ์ถ๊ณผ ๋ฉ์๋์ ํธ์ถ์ ๋ค๋ฅธ๊ฒ ์๋ค. ๋๋ค ์์๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋๋ฐ ์๋ต๋์ด ์๋์ง ์๋์ง์ ์ฐจ์ด๋ค.
์์ฑ์์ this
var funcThis = null;
function Func(){
funcThis = this;
}
var o1 = Func();
if(funcThis === window){
console.log('window');
};
var o2 = new Func();
if(funcThis === o2){
console.log('o2');
};
o1๋ window.Func()๊ฐ ์๋ต๋ ๊ฒ์ด๋ฏ๋ก window ๋ค
o2๋ new๋ฅผ ๋ถ์ฌ Func();๋ฅผ ์คํํ๊ธฐ ๋๋ฌธ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ด๋ถ์ ์ผ๋ก ๋น์ฌ์๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ๋น์ฌ์๋ ๊ฐ์ฒด๊ฐ ์์ฑ์ ์์์ this๊ฐ ๋๋ค.
์๋ก ์์ฑ๋๋ ๊ฐ์ฒด๊ฐ ์์ฑ์ ์์์ this๊ฐ ๋๋ค.
new Func(); ๊ฐ ๋ค ์คํ๋ ํ var o2์ ๋ด๊ธด๋ค.
var funcThis = null;
function Func(){
if(o2 === this){
funcThis = this;
}
}
var o2 = new Func();
if(funcThis === o2){
console.log('o2');
};
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Func()ํจ์์์ if (o2 === this) .. ๋ผ๊ณ ๋ฐ๋ก ์ธ ์ ์๋ค. func์์์ ํ ๊ฒฝ์ฐ ์์ง o2๋ผ๋ ๊ฐ์ฒด์ ๋ด๊ธฐ๊ธฐ ์ ์ ์คํ๋๊ธฐ ๋๋ฌธ์ o2๋ undefined๊ฐ ๋๋ค.
๊ฐ์ฒด์ ๋ํ ์ด๊ธฐํ๊ฐ ๋๋ ํ, ์ด๋ค ์๋ณ์์ ๋ด๊ธฐ๊ธฐ ์ ์ ๊ทธ ๊ฐ์ฒด๋ฅผ ์ฐธ๊ณ ํ ์ ์๋ reference ??
function Func(){
this.area = function(){
if(o2 === this){
return 'o2 === this';
}
}
}
var o2 = new Func();
o2.area(); //'o2 === this';
ํ์ง๋ง ์ด๋ฐ์์ผ๋ก ํ์ ๊ฒฝ์ฐ์ 'o2 === this'; ๊ฐ return ๋๋ค.
apply์ this
ํจ์๋ ๊ฐ์ฒด๋ค
์์1 - sum์ด๋ผ๋ ํจ์๊ฐ์ฒด ๋ง๋ค๊ธฐ
function sum(x,y){
return x + y;
}
sum(1,2); //3
์์2
var sum2 = new Function('x','y','return x + y;');
sum(1,2); //3
new๋ฅผ ํตํด์ Function์ด๋ผ๋ ์ด๋ฆ์ ์์ฑ์ ํจ์๋ฅผ ๋ง๋ ๊ฒ์ด๋ค. ์ฒซ๋ฒ์งธ์ธ์ x, ๋๋ฒ์งธ์ธ์ 'y'๋ ํ๋ผ๋ฏธํฐ(๋งค๊ฐ๋ณ์)๋ฅผ ์๋ฏธํ๊ณ ๋ง์ง๋ง ์ธ์๋ ํจ์์ ๋ณธ๋ฌธ์ ํด๋น๋๋ค.
์์2์ฒ๋ผ ์์ฑํ๋๊ฒ์ ๋๋จํ ๋ถํธํ๋ค. ํนํ ์์2์ ๋ง์ง๋ง ์ธ์์ฒ๋ผ ๋๋จํ ๊ธด๋ด์ฉ์ด๋ฉด ๋๋์ฑ ๋ถํธํ ๊ฒ์ด๋ค.
์์1์ฒ๋ผ ์์ฑํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ ํด์๊ธฐ๊ฐ ํจ์ ๊ฐ์ฒด๋ก ๋ง๋ค์ด ์ฃผ๋ ๊ฒ์ด๋ค. ->ํจ์ ๋ฆฌํฐ๋ด(Literal).
๋ฆฌํฐ๋ด(Literal)
//๊ฐ์ฒด ๋ฆฌํฐ๋ด
var o = {}
//ํจ์ ๋ฆฌํฐ๋ด
var arr = {}
new Object ๋ new Array๋ก ๋ช ์์ ์ผ๋ก ๋ง๋ค ์ ์์ง๋ง ๋ฆฌํฐ๋ด ๋ฐฉ์์ด๋ผ๊ณ ํธ๋ฆฌํ๊ฒ ๋ง๋ค ์ ์๊ฒํ๋ค.
var o = {};
var p = {};
function func(){
switch(this){
case o:
console.log('o');
break;
case p:
console.log('p');
break;
case window:
console.log('window');
break;
}
}
func(); //'window'
func.apply(o); //'o'
func.apply(p); //'p'
func๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์๋ค. apply์ ์ฒซ๋ฒ์งธ ์ธ์๋ก ํจ์ ํธ์ถ ์ปจํ ์คํธ(context,๋งฅ๋ฝ,๊ฐ๋ณ์ ์ด๋ค.)๋ฅผ ๋์ ํ๋ค.
์ ํต์ ์ธ ๊ฐ์ฒด์งํฅ์์ ๋ฉ์๋๋ผ๋ ๊ฒ์ ๊ฐ์ฒด์ ๊ฐํ๊ฒ ์์๋์ด ์๋ค. ํ์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ ์ฐํ๋ค.

apply๋ฅผ ์ด์ฉํด ํธ์ถ ํ๋ ํจ์ func()๋ p์ ๋ฉ์๋๋ ๋๊ณ o์ ๋ฉ์๋๋ ๋๋ค. ๊ฐ์ฒด์ ํจ์๋ ๋งค์ฐ ๋๋ฑํ๋ค. ์๋๋ฉด ํจ์๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ด๋ค. ํจ์๋ฅผ ์ด๋ป๊ฒ ํธ์ถํ๋
๊ฐ ํฌ์ธํธ๋ค. ์ด๋ป๊ฒ ํธ์ถํ๋์ ๋ฐ๋ผ ์ด๋ค ๊ฐ์ฒด์ ์ข
์๋์๋์ง๊ฐ ๋ฌ๋ผ์ง๋ค.
this๋ ๋ณํ๋ฌด์ํ๋ค. ํจ์๊ฐ ์ด๋์ ์์๋์ด ์๋์ง๊ฐ ์ค์ํ๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ ์ฐํ๋ค.
Last updated
Was this helpful?