์ผ๋ฐํจ์์ this๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ญ๊ฐ์ฒด๋ค.
์ผ๋ฐํจ์์ this๊ฐ ๊ฐ์ฒด์ ๋ฉ์๋์ผ ๊ฒฝ์ฐ์๋ง ๊ฐ์ฒด๋ฅผ ์ง์นญํ๋ค.
ํ์ดํํจ์์ ๊ฒฝ์ฐ ํด๋น ํ์ดํํจ์๊ฐ ์ํด์๋ ๊ณณ์ด ๊ฐ์ฒด์ด๋ฉด ํด๋น ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๊ณ ๊ทธ๊ฒ์ด ์๋๋ผ๋ฉด ์ ์ญ๊ฐ์ฒด์ด๋ค.
- val : 10
- a () {}
- b : function(){}2์ 3์ ๊ฐ์์๋ฏธ
- c : ()=>{}
- innerObj : {}
const obj = {
val : 10,
a(){
console.log('a',this)
},
b : function(){
console.log('b',this)
},
c : ()=>{
console.log('c',this)
},
innerObj : {
//์๋ก์ด ๊ฐ์ฒด์์ด ์กด์ฌ
innerVal : 20,
innerA(){
console.log('innerA',this)
},
innerB : function(){
console.log('innerB',this)
},
innerC : ()=>{
console.log('innerC',this)
},
}
}
console.log(obj.val)
obj.a()
obj.b()
obj.c()
console.log(obj.innerObj.innerVal)
obj.innerObj.innerA()
obj.innerObj.innerB()
obj.innerObj.innerC()
10
a {
val: 10,
a: [Function: a],
b: [Function: b],
c: [Function: c],
innerObj: {
innerVal: 20,
innerA: [Function: innerA],
innerB: [Function: innerB],
innerC: [Function: innerC]
}
}
b {
val: 10,
a: [Function: a],
b: [Function: b],
c: [Function: c],
innerObj: {
innerVal: 20,
innerA: [Function: innerA],
innerB: [Function: innerB],
innerC: [Function: innerC]
}
}
c {}
20
innerA {
innerVal: 20,
innerA: [Function: innerA],
innerB: [Function: innerB],
innerC: [Function: innerC]
}
innerB {
innerVal: 20,
innerA: [Function: innerA],
innerB: [Function: innerB],
innerC: [Function: innerC]
}
innerC {}
c์ innerC๋ ๊ฐ๋ฅํ๋ฉด ์๊ฐํ์ง๋ง์ ๋จธ๋ฆฌ๋ง ์ํ๋ค.
const obj = {
a : function(){
console.log(this)
const b = ()=>{
console.log(this);
}
function c (){
console.log(this);
}
b()
c()
}
}
obj.a()
{a: ฦ}
{a: ฦ}
Window {window: Window, self: Window, document: document, name: '', location: Location, โฆ}
- obj์ ์ํ๋ a๋ฉ์๋๋ this๋ฅผ ๊ฐ์ฒด์ธ obj๋ฅผ ์ถ๋ ฅ
- a๋ฉ์๋ ์์ ์ํ๋ ํ์ดํํจ์ b๋ ํด๋น ํ์ดํํจ์๋ฅผ ํฌํจํ๋ ๋์(a๋ฉ์๋)๊ฐ ์์นํ ๊ณณ์ด ๊ฐ์ฒด๋ผ๋ฉด ํด๋น ๊ฐ์ฒด๋ฅผ ์ง์นญํ๋ค.
- a๋ฉ์๋ ์์ ์ํ๋ ์ผ๋ฐํจ์๋ ์ผ๋ฐํจ์์ด๋ฏ๋ก window ๊ฐ์ฒด๋ฅผ ์ถ๋ ฅ
ํผ์ ํด๋ณธ ๊ฒฐ๊ณผ ๊ฐ์ฒด ์์์ ๋ฉ์๋๊ฐ 4๊ฐ์ง์ ์์์ ๋๋ ๊ฒฐ๊ณผ๊ฐ ์๋ค.
let case1 = {
value:10,
f : function(){
console.log(this);
function inner(){
console.log(this);
}
inner();
}
/*
f(){
console.log(this);
function inner(){
console.log(this);
}
inner();
}
*/
}
1๋ฒ ๊ฒฐ๊ณผ
case1.f()
์คํ
case1 ๊ฐ์ฒด์ ๋ฉ์๋์ธ f๋ ์ถ๋ ฅ๋์์ด this์ธ ์๊ธฐ์์ ์ธ case1 ๊ฐ์ฒด์ด๊ณ
inner์ ์ผ๋ฐ ๋ด์ฅํจ์์์์ this๋ ์ ์ญ๊ฐ์ฒด์ด๋ฏ๋ก window ๊ฐ์ฒด์ด๋ค
{value: 10, f: ฦ}
Window {window: Window, self: Window, document: document, name: "misaka", location: Location, โฆ}
let case2 = {
value:1000,
f : function(){
console.log(this);
let inner = ()=>{console.log(this)};
inner();
}
/*
f(){
console.log(this);
let inner = ()=>{console.log(this)};
inner();
}
*/
}
2๋ฒ ๊ฒฐ๊ณผ
case1.f()
์คํ
1๋ฒ ๊ฒฐ๊ณผ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฒซ๋ฒ์งธ this๋ case2 ๊ฐ์ฒด์ด๋ค.
inner๋ ํ์ดํํจ์์ด๋ฏ๋ก ์ด๋์ this๋ inner ํจ์๊ฐ ํด๋น ์์น์์ ๊ฐ๋ this์ด๋ฏ๋ก ์ฒซ๋ฒ์งธ this์ ๊ฐ์ case2 ๊ฐ์ฒด์ด๋ค.
{value: 1000, f: ฦ}f: ฦ ()value: 1000__proto__: Object
{value: 1000, f: ฦ}f: ฦ ()value: 1000__proto__: Object
let case1 = {
value:10,
f :{
inner(){
console.log(this);
}
}
}
case1.f.inner()
์คํ์ this์ธ {inner: ฦ}
๊ฐ ์ถ๋ ฅ
let case3 = {
f : ()=>{
console.log(this);
}
}
3๋ฒ ๊ฒฐ๊ณผ
case1.f()
์คํ
case3์ ์ผ๋ฐ์ ์ธ ๋ฉ์๋๋ก ์ฐ๊ฒฐ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฅ window ๊ฐ์ฒด์ด๋ค.
Window {window: Window, self: Window, document: document, name: "misaka", location: Location, โฆ}
์๋ํ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
const obj = {
firstMethod(){
function secondFunc(){
console.log(this)
}
const secondArrowFunc = ()=>{
console.log(this)
}
secondFunc()
secondArrowFunc()
},
firstObj : {
secondMethod(){
function thirdFunc(){
console.log(this)
}
const thirdArrowFunc = () => {
console.log(this)
}
thirdFunc()
thirdArrowFunc()
},
secondObj : {
thirdMethod(){
function fourFunc(){
console.log(this)
}
const fourArrowFunc = () => {
console.log(this)
}
fourFunc()
fourArrowFunc()
}
}
}
}
obj.firstMethod()
obj.firstObj.secondMethod()
obj.firstObj.secondObj.thirdMethod()
ํด๋น๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
๊ฐ๊ฐ์ ์ผ๋ฐํจ์์์์์ ์ผ๋ฐ ๋ด์ฅํจ์์ this๋ window๊ฐ์ฒด์ด๋ฏ๋ก 3๊ฐ๋ window๊ฐ์ฒด๊ฐ ๋์๊ณ
2๋ฒ์งธ ๊ฒฐ๊ณผ๋
secondArrowFunc()
์ ๊ฒฐ๊ณผ๋ก ํด๋น this์ ์์ ์์น์ธ
firstMethod()
์์น์ this์ด๋ฏ๋ก ๊ฐ์ฒด์ธ obj์ผํ ๊ณ ํด๋น obj๋ firstObj์ firstMethod๋ฅผ ๊ฐ์ ธ ์ด์๊ฐ๋ค.
4๋ฒ์งธ ๊ฒฐ๊ณผ๋
thirdArrowFunc()
์ ๊ฒฐ๊ณผ๋ก ํด๋น this์ ์์ ์์น์ธ
secondMethod()
์์น์ this์ด๋ฏ๋ก ๊ฐ์ฒด์ธ firstObj์ผํ ๊ณ ํด๋น firstObj๋ secondObj์ secondMethod๋ฅผ ๊ฐ์ ธ ์ด์๊ฐ๋ค.
6๋ฒ์งธ ๊ฒฐ๊ณผ๋
thirdArrowFunc()
์ ๊ฒฐ๊ณผ๋ก ํด๋น this์ ์์ ์์น์ธ
secondMethod()
์์น์ this์ด๋ฏ๋ก ๊ฐ์ฒด์ธ secondObj์ผํ ๊ณ ํด๋น secondObj๋ thirdObj๋ฅผ ๊ฐ์ ธ ์ด์๊ฐ๋ค.