๐ก This : ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ํค์๋ this์ ๊ฐ์ ํจ์๋ฅผ ํธ์ถํ ์์น์์ ๊ฒฐ์ ๋จ.
1. ๋จ๋ ์ผ๋ก ์ฐ์ธ this
this๋ฅผ ํธ์ถํ๋ ๊ฒฝ์ฐ์ global object๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๋ธ๋ผ์ฐ์ ์์ ํธ์ถํ๋ ๊ฒฝ์ฐ ⇒ [object Window]
ES5 strict mode(์๊ฒฉ ๋ชจ๋)์์๋ ๋ง์ฐฌ๊ฐ์ง
'use strict';
let x = this;
console.log(x); //Window
2. ํจ์ ์์์ ์ด this
ํจ์ ์์์ this๋ ํจ์์ ์ฃผ์ธ(window๊ฐ์ฒด)์๊ฒ ๋ฐ์ธ๋ฉ
- sloppy mode
function myFunction() {
return this;
}
console.log(myFunction()); //Window
let num = 0; // (์ ์ญ๋ณ์)
function addNum() {
this.num = 100; // window๊ฐ์ฒด
num++;
console.log(num); // 101
console.log(window.num); // 101
console.log(num === window.num); // true
}
addNum();
- strict mode
"use strict";
function myFunction() {
return this;
}
console.log(myFunction()); //undefined
let num = 0;
function addNum() {
this.num = 100; //ERROR! Cannot set property 'num' of undefined
num++;
}
addNum();
3. ๋ฉ์๋ ์์์ ์ด this
๋ฉ์๋ ํธ์ถ ์ ๋ฉ์๋ ๋ด๋ถ ์ฝ๋์์ ์ฌ์ฉ๋ this๋ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ
let person = {
firstName: 'John',
lastName: 'Doe',
fullName: function () {
return this.firstName + ' ' + this.lastName;
},
};
person.fullName(); //"John Doe"
let num = 0;
function showNum() {
console.log(this.num);
}
showNum(); //0
let obj = {
num: 200,
func: showNum,
};
obj.func(); //200
4. ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์์ ์ด this
์ด๋ฒคํธ ํธ๋ค๋ฌ์์ this๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ๋ HTML ์์๋ก ๋ฐ์ธ๋ฉ
let btn = document.querySelector('#btn')
btn.addEventListener('click', function () {
console.log(this); //#btn
});
5. ์์ฑ์ ์์์ ์ด this
์์ฑ์ ํจ์๊ฐ ์์ฑํ๋ ๊ฐ์ฒด๋ก this๊ฐ ๋ฐ์ธ๋ฉ
function Person(name) {
this.name = name;
}
let kim = new Person('kim');
let lee = new Person('lee');
console.log(kim.name); //kim
console.log(lee.name); //lee
new ํค์๋๋ฅผ ๋นผ๋จน๋ ์๊ฐ ์ผ๋ฐ ํจ์ ํธ์ถ๊ณผ ๊ฐ์์ง๊ธฐ ๋๋ฌธ์, ์ด ๊ฒฝ์ฐ๋ this๊ฐ window์ ๋ฐ์ธ๋ฉ
let name = 'window';
function Person(name) {
this.name = name;
}
let kim = Person('kim');
console.log(window.name); //kim
6. ํ์ดํ ํจ์๋ก ์ด this
ํจ์ ์์์ this๊ฐ ์ ์ญ ๊ฐ์ฒด๋ก ์ ์ฐ๊ณ ์ถ์ ๋
ํ์ดํ ํจ์๋ ์ ์ญ ์ปจํ ์คํธ์์ ์คํ๋๋๋ผ๋ this๋ฅผ ์๋ก ์ ์ํ์ง ์๊ณ , ๋ฐ๋ก ๋ฐ๊นฅ ํจ์๋ ํด๋์ค์ this๋ฅผ ์ฌ์ฉ
let Person = function (name, age) {
this.name = name;
this.age = age;
this.say = function () {
console.log(this); // Person {name: "Nana", age: 28}
setTimeout(function () {
console.log(this); // Window
console.log(this.name + ' is ' + this.age + ' years old');
}, 100);
};
};
let me = new Person('Nana', 28);
me.say(); // undefined is undefined years old
⇒
let Person = function (name, age) {
this.name = name;
this.age = age;
this.say = function () {
console.log(this); // Person {name: "Nana", age: 28}
setTimeout(() => {
console.log(this); // Person {name: "Nana", age: 28}
console.log(this.name + ' is ' + this.age + ' years old');
}, 100);
};
};
let me = new Person('Nana', 28);
me.say(); //Nana is 28 years old
7. ๋ช ์์ ๋ฐ์ธ๋ฉ์ ํ this
๋ช ์์ ๋ฐ์ธ๋ฉ : ํด๋น this๋ฅผ ์ง์ผ๋ก ์ง์ ํ๋ ๊ฒ
apply(), call(), bind() ๋ฉ์๋๋ Function Object์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์๋ ๋ฉ์๋์ธ๋ฐ์, ์ธ์๋ฅผ this๋ก ๋ง๋ค์ด์ฃผ๋ ๊ธฐ๋ฅ์ ํ๋ค.
apply()์ call()์ ๋ณดํต ์ ์ฌ๋ฐฐ์ด ๊ฐ์ฒด์๊ฒ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฐ๊ณ ์ ํ ๋ ์ฌ์ฉ
function whoisThis() {
console.log(this);
}
whoisThis(); //window
let obj = {
x: 123,
};
whoisThis.call(obj); //{x:123}
apply() ํ์ฉ 1 ⇒ 2
function Character(name, level) {
this.name = name;
this.level = level;
}
function Player(name, level, job) {
this.name = name;
this.level = level;
this.job = job;
}
function Character(name, level) {
this.name = name;
this.level = level;
}
function Player(name, level, job) {
Character.apply(this, [name, level]);
this.job = job;
}
let me = new Player('Nana', 10, 'Magician');
call and apply
function add(a, b) {
return a + b;
}
console.log(add.call(null, 1, 2)); // 3
console.log(add.apply(null, [1, 2])); // 3
'์ธ์ด > Script Question' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Web Storage๋ ๋ฌด์์ธ๊ฐ (0) | 2024.03.31 |
---|---|
IoC / DI๊ฐ ๋ฌด์์ธ๊ฐ (0) | 2024.03.31 |
ํ์ดํ ํจ์ VS ๋๋ค ํจ์ (1) | 2023.10.29 |
๊ดํธ์ ์ฉ๋ (0) | 2023.10.29 |
Undefined์ Null์ ์ฐจ์ด (0) | 2023.10.29 |