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}
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[FE/JavaScript] ๊ตฌ์กฐ ๋ถํด ํ ๋น(๋์คํธ๋ญ์ฒ๋ง ํ ๋น) (0) | 2022.07.06 |
---|---|
[FE/JavaScript] strict mode (0) | 2022.06.27 |
[FE/JavaScript] ์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ (0) | 2022.06.13 |
JavaScript - Event (0) | 2022.05.03 |
JavaScript - DOM(Document Object Model) (0) | 2022.05.03 |
๋๊ธ