JS. this ๊ฐ’

1TBhardยท2020๋…„ 12์›” 16์ผ
0

FrontEnd๋ฉด์ ‘

๋ชฉ๋ก ๋ณด๊ธฐ
5/7
post-thumbnail

๐Ÿ“Œ "this๋Š” JavaScript์—์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."


JS์—์„œ์˜ this ๊ฐ’

C++, JAVA์™€ ๊ฐ™์€ ์–ธ์–ด์—์„œ this๋Š” ์ž๊ธฐ์ž์‹  ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ JavaScript ์˜ this ๊ฐ’์€ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ thisValue ์— ์˜ํ•ด ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

thisValue๋Š” ์•„๋ž˜์˜ 5๊ฐ€์ง€ ํŒจํ„ด์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค.

  • ์ „์—ญ(global)
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ(function Invocation)๊ณผ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ(Method Invocation)
  • call(), apply(), bind()
  • ์ƒ์„ฑ์ž(Construction)
  • strict ๋ชจ๋“œ

๐Ÿ’ก ๋ฐ”์ธ๋”ฉ : ์‹ค์ œ ๊ฐ’์ด ๋ณ€์ˆ˜์— ๋ฌถ์ด๋Š” ๊ฒƒ
๐Ÿ’ก strict ๋ชจ๋“œ : ES5 ์— ์žˆ๋Š” ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์œผ๋กœ ์—„๊ฒฉํ•˜๊ฒŒ ๋ฌธ๋ฒ•์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋ชจ๋“œ์ด๋‹ค.


1. ์ „์—ญ(global)

์ „์—ญ์—์„œ this๋Š” ์ „์—ญ ์ปจํ…์ŠคํŠธ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ this ๊ฐ’์€ ์ „์—ญ ๊ฐ์ฒด ๊ฐ€ ๋œ๋‹ค.

// ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” window ๊ฐ€ ์ „์—ญ๊ฐ์ฒด ์ด๋‹ค.
console.log(this === window);           // true

this.myString = "hello?";
console.log(window.myString);           // "hello?"
console.log(myString);                  // ์ „์—ญ ๋ณ€์ˆ˜ myString์„ ์ถœ๋ ฅ: "hello?"

๐Ÿ’ก NodeJS ์˜ ๊ฒฝ์šฐ, ์ „์—ญ์—์„œ์˜ this ๊ฐ’์€ global ์ด ๋œ๋‹ค.


2. ํ•จ์ˆ˜ ํ˜ธ์ถœ๊ณผ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this ๊ฐ’์€ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ์„œ ๊ฒฐ์ •๋œ๋‹ค.

๋‹จ์ˆœํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ๋‚˜, ๋‚ด๋ถ€ํ•จ์ˆ˜, ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ this ๋Š” ์ „์—ญ ๊ฐ์ฒด ์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

function foo() {
  console.log("foo's this: ",  this);    // Window
  function bar() {
    console.log("bar's this: ", this);   // Window
  }
  bar();
}
foo();

๊ทธ๋Ÿฌ๋‚˜ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ์•ˆ์—์„œ this๋Š” ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
ํ•˜์ง€๋งŒ ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋Š” ์ „์—ญ ๊ฐ์ฒด์— this ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

var value = 1;

const obj = {
  value: 100,
  qux: function() {
     console.log(this.value);         // this === obj, 100 ์ถœ๋ ฅ
     setTimeout(
       // ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜
       function() {
         console.log(this);           // this === Window, Window ์ถœ๋ ฅ
         console.log(this.value);     // 1 ์ถœ๋ ฅ
       }, 0);
  }
}
 
obj.qux();

3. call(), apply(), bind()

call(), apply(), bind() ์‚ฌ์šฉํ•˜์—ฌ this ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • call(): ์ด๋ฏธ ํ• ๋‹น๋˜์–ด ์žˆ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ํ•จ์ˆ˜/๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•ด๋‹น ๊ฐ์ฒด์— ์žฌํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉ
  • apply(): call() ๊ณผ ๋น„์Šทํ•˜๋‚˜ ์ธ์ˆ˜๋“ค์„ ๋‹จ์ผ ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค.
  • bind(): this ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ ๋œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
var value = 20;

function mySum(num) {
  // this.value ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
  if(this.value == undefined) this.value = 50;
  
  return this.value + num;
}

// this ๊ฐ’์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„ global ๊ฐ์ฒด(window)๊ฐ€ this๊ฐ€ ๋จ
console.log(mySum(2));                  // 22

// call()
console.log(mySum.call(mySum, 2));      // 52

// apply()
console.log(mySum.apply(mySum, [9]));   // 59
            
// bind()
var bindMySum = mySum.bind(mySum);
console.log(bindMySum(50));             // 100

4. ์ƒ์„ฑ์ž(Construction)

์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋กœnew ์—ฐ์‚ฐ์ž, class์˜ constructor๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ this ๋ฐ”์ธ๋”ฉ์ด ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๋œ๋‹ค.

function test(number) {
	this.num = number;
}

// new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ
var obj = new test(1);
console.log(obj.num);        // 1

/* ================ ๋งŒ์•ฝ์— new ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ obj๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ================ */
// ํ•จ์ˆ˜๋‚ด์—์„œ this.num ์€ window.num ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.
console.log(obj.num);        // Uncaught TypeError

์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

  1. ๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ this ๋ฐ”์ธ๋”ฉ
    • ํ•จ์ˆ˜ ์ƒ์„ฑ์ „ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ(์ด๋•Œ this๊ฐ€ ์ด ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด)
    • ์ƒ์„ฑ๋œ ๋นˆ ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ prototype ์„ ์ž์‹ ์˜ prototype ์œผ๋กœ ์„ค์ •ํ•จ
  2. this.๋ฉ”์„œ๋“œ, this.์†์„ฑ ๋“ฑ this๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ์†์„ฑ์™€ ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•จ
  3. ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
    • ๋ฐ˜ํ™˜๋ฌธ(return)์ด ์—†์œผ๋ฉด ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
    • ๋ฐ˜ํ™˜๋ฌธ์— this ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋จ
function Obj(num) {
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ฝ”๋“œ ์‹คํ–‰ ์ „ ---------- 1
  this.num= num;         // ---------- 2
  // ์ƒ์„ฑ๋œ ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ ------------------3
}

var me = new Obj(33);
console.log(me.num);

๋”ฐ๋ผ์„œ, new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ
๊ฐ์ฒด๋ฅผ ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์œผ๋ฉฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

๐Ÿ“– ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด vs ํ•จ์ˆ˜

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹๊ณผ ํ•จ์ˆ˜ ๋ฐฉ์‹์˜ ์ฐจ์ด๋Š” [[Prototype]](ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด) ์— ์žˆ๋‹ค.

// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹
// [[Prototype]] : Object.prototype
var obj = {
	name: "dulli",
	age: 100
}

// ํ•จ์ˆ˜ ๋ฐฉ์‹
// [[Prototype]] : obj.prototype
function obj2() {
	this.name: "ddochi",
	age: 35
}
var objIns = new obj2();

5. strict ๋ชจ๋“œ

strict ๋ชจ๋“œ ๋ผ๋ฉด this ๊ฐ’์€ undefined ๊ฐ€ ๋œ๋‹ค.

// ํ•จ์ˆ˜ ๋‚ด์—์„œ use strict ๋ชจ๋“œ ํ™œ์„ฑํ™” 
function test() {
  "use strict";
  return this;         // ์—ฌ๊ธฐ์„œ this๋Š” undefined
}

console.log(this);     // Window, ์ „์—ญ ๊ฐ์ฒด
console.log(test());   // undefined

๊ฒฐ๋ก 

JavaScript์—์„œ๋Š” this ๊ฐ’์€ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ thisValue ์— ์˜ํ•ด ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.
thisValue ๋Š” ์•„๋ž˜์˜ 5๊ฐ€์ง€ ํŒจํ„ด์— ์˜ํ•ด ๊ฒฐ์ •์ด๋œ๋‹ค.

  • ์ „์—ญ(global) : this === ์ „์—ญ๊ฐ์ฒด
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ(function Invocation)๊ณผ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ(Method Invocation)
    • ๋‹จ์ˆœํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ : this === ์ „์—ญ๊ฐ์ฒด
    • ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด(๋ฉ”์„œ๋“œ ํ˜ธ์ถœ);
      • ๋ฉ”์„œ๋“œ์•ˆ์—์„œ this === ์ž๊ธฐ์ž์‹ 
      • ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜์˜ this === ์ „์—ญ๊ฐ์ฒด
  • call(), apply(), bind() : this ๊ฐ’์ด ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ๋จ
  • ์ƒ์„ฑ์ž(Construction) : this === ์ž๊ธฐ์ž์‹ 
  • strict ๋ชจ๋“œ : this === undefined


์ฐธ์กฐ

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/this
https://hyunseob.github.io/2016/03/10/javascript-this/
https://muckycode.blogspot.com/2015/04/javascript-this.html
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/์• ๋กœ์šฐ_ํŽ‘์…˜

profile
๊ธฐ์–ต์„ ๋„˜์–ด ์Šต๊ด€์œผ๋กœ

0๊ฐœ์˜ ๋Œ“๊ธ€