๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Language/JavaScript

[FE/JavaScript]this

by DevIseo 2022. 6. 23.

like this yo like this

Q.this๊ฐ€ ๋ญ”๊ฐ€์š”?

this๋Š” ์ž๊ธฐ์ฐธ์กฐ๋ณ€์ˆ˜

์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋‚˜ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜(self-referencing variable)

๐Ÿ˜ฒq**.์™œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ?**

  • ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ(ํ”„๋กœํผํ‹ฐ)๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ์ด๋•Œ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด ๋จผ์ € ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ!

+)this์˜ ํŠน์ง•

  • this๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์— ์˜ํ•ด ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, ์ฝ”๋“œ ์–ด๋””์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅ!
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด arguments ๊ฐ์ฒด์™€ this๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ „๋‹ฌ
  • this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’, ์ฆ‰ this binding์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๋™์ ์œผ๋กœ ๊ฒฐ์ • (ํด๋ž˜์Šค๊ธฐ๋ฐ˜์˜ ์–ธ์–ด(C++,JAVA์˜ ๊ฒฝ์šฐ this๋Š” ์–ธ์ œ๋‚˜ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด)
  • ๋˜ํ•œ, strict mode(์—„๊ฒฉ๋ชจ๋“œ) this binding์— ์˜ํ–ฅ์„ ์ค€๋‹ค!

this๋Š” ์ฝ”๋“œ ์–ด๋””์—์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅ(์ „์—ญ์—์„œ๋„, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋„ ์ฐธ์กฐ)

//this๋Š” ์–ด๋””์„œ๋“ ์ง€ ์ฐธ์กฐ ๊ฐ€๋Šฅ
//์ „์—ญ์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด์ธ window๋ฅผ ๊ฐ€๋ฆฌํ‚ด

console.log(this) //window

function square(number){
//์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚ด
	console.log(this) //window
	return number*number
}

square(2)

const person={
	name:'yu',
	getName(){
	//๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด!
		console.log(this) // {name:'yu',getName:f}
		return this.name
	}
}

console.log(person.getName())//yu

function Person(name){
	this.name = name
	//์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค!
	console.log(this)//Person{name:'yu'}
}
const me = new Person('yu')

๐Ÿšจthis๋Š” ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€ ๋˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ์Œ!

  • ๋”ฐ๋ผ์„œ, strict mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ+) strict modehttps://beomy.tistory.com/13
  • strict mode๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ฌต์ธํ–ˆ๋˜ ์—๋Ÿฌ๋“ค์˜ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœ์ƒ์‹œํ‚ด!
  • (์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ)

<aside> ๐Ÿ’ก โœsummary

  • this๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜.
  • this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค! </aside>

Q.this ๋ฐ”์ธ๋”ฉ์ด๋ž€?

๋ฐ”์ธ๋”ฉ์ด๋ž€?

๋ฐ”์ธ๋”ฉ์ด๋ž€ ์‹๋ณ„์ž์™€ ๊ฐ’์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ณผ์ •!

ex) ๋ณ€์ˆ˜ ์„ ์–ธ์€ ๋ณ€์ˆ˜ ์ด๋ฆ„(์‹๋ณ„์ž)๊ณผ ํ™•๋ณด๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๊ฒƒ

<aside> ๐Ÿ’ก ๊ทธ๋ ‡๋‹ค๋ฉด this ๋ฐ”์ธ๋”ฉ์€?

this(ํ‚ค์›Œ๋“œ๋กœ ๋ถ„๋ฅ˜๋˜์ง€๋งŒ ์‹๋ณ„์ž ์—ญํ• ์„ ํ•จ!)์™€ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๊ฒƒ

</aside>

Q.this๋Š” ๋™์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ๋ฐ”์ธ๋”ฉ๋˜๋Š” ๊ฐ์ฒด๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅด๋‚˜์š”?

<summary>

<aside> ๐Ÿ’ก this ๋ฐ”์ธ๋”ฉ(this์— ๋ฐ”์ธ๋”ฉ ๋  ๊ฐ’)์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹,

์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •

</aside>

ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹ this ๋ฐ”์ธ๋”ฉ

์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „์—ญ ๊ฐ์ฒด
๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค
Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด

<code>

//this binding์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •

const foo = function(){
	console.dir(this)
}

//๋™์ผํ•œ ํ•จ์ˆ˜๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ
//1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this -> ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚ด

foo() //window

//2.๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜๋ฅผ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•˜์—ฌ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด obj๋ฅผ ๊ฐ€๋ฆฌํ‚ด
const obj ={foo}
obj.foo() //obj

//3.์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด
new foo() // foo

//4.Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ
//foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ธ์ˆ˜์— ์˜ํ•ด ๊ฒฐ์ •

const bar = {name:'bar'}
foo.call(bar) //bar
foo.apply(bar) //bar
foo.bind(bar) //bar

๐Ÿ˜Ž๋” ๋ณด ๊ธฐ


this binding์„ ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”!

1.์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ


  • ๊ธฐ๋ณธ์ ์œผ๋กœ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ binding
  • ์–ด๋– ํ•œ ํ•จ์ˆ˜๋ผ๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด this์— ์ „์—ญ ๊ฐ์ฒด๊ฐ€ binding
function foo(){
	console.log("foo's this: ",this) //window
	function bar(){
		console.log("bar's this: ",this) //window
	}
	bar()
}
foo()
  • ์ „์—ญ ํ•จ์ˆ˜๋Š” ๋ฌผ๋ก ์ด๊ณ  ์ค‘์ฒฉ ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ์‹œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ binding
    function foo(){
    	'use strict'
    	
    	**console.log("foo's this**:", this) //undefined
    	function bar(){
    		console.log("bar's this:", this)//undefined
    	}
    	bar()
    }
    foo()
    
  • +) strict mode ๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” undefined๊ฐ€ binding
  • +)์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด?⇒์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this ๋ฐ”์ธ๋”ฉ์„ ๋ฉ”์„œ๋“œ์˜ this ๋ฐ”์ธ๋”ฉ๊ณผ ์ผ์น˜ ์‹œํ‚ด
  • var value = 1 const obj = { value:100, foo(){ //this ๋ฐ”์ธ๋”ฉ(obj)์„ ๋ณ€์ˆ˜ that์— ํ• ๋‹น const that = this //์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this ๋Œ€์‹  that์„ ์ฐธ์กฐ setTimeout(function(){ console.log(that.value) //100 },100) } } obj.foo() //⇒์ด ๋ฐฉ๋ฒ• ์™ธ์— JS๋Š” this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•  ์ˆ˜ ์žˆ๋Š” //`Function.prototype.apply`,`Function.prototype.call`, //`Function.prototype.bind`๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต
  • ⇒ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋„ ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ!

2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ


  • ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์•ž์˜ ๋งˆ์นจํ‘œ(.) ์—ฐ์‚ฐ์ž ์•ž์— ๊ธฐ์ˆ ํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ
  • ๐Ÿšจ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์†Œ์œ ํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋จ!

2.1 ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this

const person = {
	name:'Lee',
	getName(){
	//๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this -> ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— binding
		return this.name //this๋Š” person์ด ๋˜๊ฒŒ ๋จ!
	}
}

//๋ฉ”์„œ๋“œ getName์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด : person
console.log(person.getName()) //Lee

--------------------------------------------
const anotherPerson(){
	name:'Kim',
}
//getName ๋ฉ”์„œ๋“œ๋ฅผ anotherPerson ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ• ๋‹น!
anotherPerson.getName = person.getName
//getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” anotherPerson
console.log(anotherPerson.getName()) //Kim

//getName ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹น
const getName = person.getName
//getName ๋ฉ”์„œ๋“œ๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ!
console.log(getName()) //''

/*์™œ ''์ด ์ถœ๋ ฅ๋˜๋‚˜?
์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ getName ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this.name์€ ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name๊ณผ ๊ฐ™์Œ
๋”ฐ๋ผ์„œ, ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ์˜ window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ
---->๊ธฐ๋ณธ๊ฐ’์€ ''์ด๋‹ค!
*/

//node.js ํ™˜๊ฒฝ์—์„œ this.name === undefined

2.2 ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ์™€ this

function Person(name){
	this.name = name
}

Person.prototype.getName = function(){
	return this.name
}

const me = new Person('Lee')

//getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด : me
console.log(me.getName()) //Lee

Person.prototype.name = 'Kim'
//getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” Person.prototype
cosole.log(Person.prototype.getName) //'Kim'

์ฆ‰, this์— ๋ฐ”์ธ๋”ฉ ๋  ๊ฐ์ฒด๋Š” ํ˜ธ์ถœ ์‹œ์ ์— ๊ฒฐ์ •๋œ๋‹ค!

3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ


  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ
//์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius){
	//์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด
	this.radius = radius
	this.getDiameter = function(){
		return 2*this.radius
	}
}

//๋ฐ˜์ง€๋ฆ„์ด 5์ธ Circle ๊ฐ์ฒด ์ƒ์„ฑ
const circle1 = new Circle(5)
//๋ฐ˜์ง€๋ฆ„์ด 10์ธ Circle ๊ฐ์ฒด ์ƒ์„ฑ
const circle2 = new Circle(10)

console.log(circle1.getDiameter())    //10
console.log(circle2.getDiameter())    //20

------------------------------------
//new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋™์ž‘ x =>์ฆ‰, ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
const circle3 = Circle(15)
//์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Circle์—๋Š” ๋ฐ˜ํ™˜๋ฌธ์ด ์—†์Œ! ์•”๋ฌต์ ์œผ๋กœ undefined ๋ฐ˜ํ™˜
console.log(circle3)      //undefined
//์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Circle ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด!
console.log(radius)      //15

4. Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ


  • apply, call, bind๋ฉ”์„œ๋“œ๋Š” Function.prototype ๋ฉ”์„œ๋“œ
  • ์ด ๋ฉ”์„œ๋“œ๋“ค์€ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

<apply,call>

  • apply ๋ฉ”์„œ๋“œ์™€ call ๋ฉ”์„œ๋“œ์˜ ๋ณธ์งˆ์ ์ธ ๊ธฐ๋Šฅ
    • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ
  • apply์™€ call ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ํŠน์ • ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉ
function getThisBinding(){
	return this
}

//this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
const thisArg = {a:1}
console.log(getThisBinding()) //window

//getThisBinding ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด๋ฅผ getThisBinding ํ•จ์ˆ˜์˜ this ๋ฐ”์ธ๋”ฉ
console.log(getThisBinding.apply(thisArg)) //{a:1}
console.log(getThisBinding.call(thisArg)) //{a:1}

<bind>

  • apply์™€ call ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ
  • ๋‹ค๋งŒ, ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ’์œผ๋กœ this ๋ฐ”์ธ๋”ฉ์ด ๊ต์ฒด๋œ ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜
function getThisBinding(){
	return this
}
// this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
const thisArg = {a:1}

//bind ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ thisArg๋กœ this ๋ฐ”์ธ๋”ฉ์ด ๊ต์ฒด๋œ
//getThisBinding ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜
console.log(getThisBinding.bind(thisArg)) //getThisBinding
//bind๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋Š” ์•Š์œผ๋ฏ€๋กœ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค
console.log(getThisBinding.bind(thisArg()) //{a:1}

๋Œ“๊ธ€