MellowHoney 2023. 10. 30. 10:13
728x90
๋ฐ˜์‘ํ˜•
๐Ÿ’ก 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

https://nykim.work/71

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
๋ฐ˜์‘ํ˜•