https://poiemaweb.com/es6-arrow-function
Arrow function | PoiemaWeb
Arrow function(ํ์ดํ ํจ์)์ function ํค์๋ ๋์ ํ์ดํ(=>)๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋ตํ ๋ฐฉ๋ฒ์ผ๋ก ํจ์๋ฅผ ์ ์ธํ ์ ์๋ค. ํ์ง๋ง ๋ชจ๋ ๊ฒฝ์ฐ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ์๋๋ค. ๋ฌธ๋ฒ์ ์๋์ ๊ฐ๋ค.
poiemaweb.com
ใด> ๊ฐ์ฅ ์ ํํ๊ณ ์์ธํ๊ณ ํ๋ถํ ์์๋ก ์ค๋ช ๋์ด ์๋ ๋ฌธ์
const obj = {
name: '๊ผฌ๋ถ๊ธฐ',
sayName: () => {
console.log(this.name);
}
}
obj.sayName();
class Counter {
name = 'counter';
count = 0;
increase = () => {
console.log(this);
}
obj = {
name: '๊ผฌ๋ถ๊ธฐ',
sayName: () => {
console.log(this.name);
}
}
}
const counter = new Counter();
console.log('counter.increase()');
counter.increase(); //this ๋ Counter
counter.obj.sayName(); //this๋ counter
temp.js:4
temp.js:24 counter.increase()
temp.js:13 Counter
temp.js:18 counter
๋ค๋ฅธ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ this ๋ ํด๋์ค๋ฅผ ํตํด ์์ฑํ ์ธ์คํด์ค(์ค๋ธ์ ํธ) ์์ ์ ๊ฐ๋ฅด์ผฐ๋ค.
- this๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์(self-reference variable)์ด๋ค.
- this๋ฅผ ํตํด ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
์ฆ, ๋ง๋ค์ด์ง ๊ฐ์ฒด ๊ทธ ์์ ์ด์๋ค.
๊ทธ๋ฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ this ๋ ์๋ผ๋ ์์ ๋จํ์ ์ง๋(;;;)์ด๋ค.
๋๊ฐ ๋ถ๋ฅด๋์ ๋ฐ๋ผ์ this์ ์ฃผ์ธ์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ด๋ค.
๋๊ฐ ํธ์ถํ๋์ง,
ํธ์ถ ๋ฌธ๋งฅ์ ๋ฐ๋ผ์ this๊ฐ ๋์ ์ผ๋ก ๋ณ๊ฒฝ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ธ์ด์ ๋นํด์ ์๋ฐ์คํฌ๋ฆฝํธ์ this๋ ์ ๋ ๋ ํท๊ฐ๋ฆด ์ ์๋ค.
๊ทธ๋ฐ๋ฐ ํ์ดํ ํจ์๋ ๋ ๋ค๋ฅด๋ค.
ํ์ดํ ํจ์๋ ์ ์ธํ ์์ ์ ๋ถ๋ชจ ์ค์ฝํ๊ฐ this์ด๋ค. ๊ณ ๋ก ๋ถ๋ณ์ด๋ค.(;;;)
console.log(this); //this ๋ window
function func() {
console.log(this);
}
func(); //this ๋ window
window.func(); //์๋, ๊ธ๋ก๋ฒ์์ ํจ์๋ฅผ ํธ์ถํ๋ค๋ ๊ฒ์ ์๋์ฐ์์ ํธ์ถํ๋ค๋ ๊ฒ๊ณผ ๊ฐ๊ธฐ ๋๋ฌธ
class Counter {
count = 0;
increase = function () {
console.log(this);
}
}
const counter = new Counter();
counter.increase(); //this ๋ Counter
const tempIncrease = counter.increase;
tempIncrease(); //this ๋ undefined
const counter = new Counter();
counter.increase(); //this ๋ Counter
Counter์์ increase()๋ฅผ ํธ์ถํ์ผ๋ฏ๋ก ์ฌ๊ธฐ๊น์ง๋ง ํด๋ this๋ ํด๋์ค๋ฅผ ๊ฐ๋ฅดํค๊ณ ์์์ผ๋,
const tempIncrease = counter.increase;
tempIncrease(); //this ๋ undefined
tempIncrease๋ผ๋ ๋ณ์์ ํ ๋นํ ์๊ฐ,
๊ทธ ์ด๋ ํ ๊ฐ์ฒด๊ฐ tempIncrease()๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ์๋๊ธฐ์ this์ ์ฃผ์ธ์ ์์ค๋๊ณ
undefined๊ฐ ์ถ๋ ฅ๋๊ฒ ๋๋ค.(let๊ณผ const๋ก ์ ์ธ๋ ๋ณ์๋ ์๋์ฐ์ ๋ฑ๋ก๋์ด์์ง ์๋ค. ๊ทธ๋์ ์๋์ฐ๊ฐ ํธ์ถ ํ๋ ๊ฒ๋, ๊ทธ ์ด๋ ํ ์ค๋ธ์ ํธ๊ฐ ํธ์ถํ๋ ๊ฒ๋ ์๋ ๊ฒ์ด ๋ ๊ฒ์ด๋ค.)
console.log(this); //this ๋ window
function func() {
console.log(this);
}
func(); //this ๋ window
class Counter {
count = 0;
increase = function () {
console.log(this);
}
}
const counter = new Counter();
counter.increase(); //this ๋ Counter
const tempIncrease = counter.increase;
tempIncrease(); //this ๋ undefined
class CrazyJs {
}
const crazyJavascript = new CrazyJs();
crazyJavascript.func = counter.increase;
crazyJavascript.func(); //this๋ CrazyJs
์์ ์ํฉ์์ ํ์ดํ ํจ์๋ก ๋ฐ๊พธ์ด๋ณด์๋ค.
class Counter {
count = 0;
increase = () => {
console.log(this);
}
}
const counter = new Counter();
console.log('counter.increase()');
counter.increase(); //this ๋ Counter
const tempIncrease = counter.increase;
console.log('tempIncrease()');
tempIncrease(); //this ๋ undefined
class CrazyJs {
}
const crazyJavascript = new CrazyJs();
crazyJavascript.func = counter.increase;
console.log('crazyJavascript.func()');
crazyJavascript.func(); //this๋ CrazyJs
counter.increase()
Counter
tempIncrease()
Counter
crazyJavascript.func()
Counter
์ฌ๊ธฐ์ ์์ฃผ ์ค์ํ ํฌ์ธํธ๋, ์ฌํ this๋ ํธ์ถ ์์ ์ ๊ฒฐ์ ๋๋๋ฐ ํ์ดํํจ์๋ ๊ทธ์ ์ ์ธํ ๋ถ๋ชจ ์ค์ฝํ๋ฅผ ๋ this๋ก ์ ์งํ๋ค๋ ์ ์ด๋ค.
ํ์ดํ ํจ์ : ๋ถ๋ชจ ์ค์ฝํ(์ ์ธ ๊ธฐ์ค)
ํ์ดํ ํจ์์ธ์ง ์๋์ง๋ this๋ฅผ ๊ฒฐ์ ํ๋๋ฐ ์ํฅ์ ๋ผ์น๋ค.
//๊ฐ์ฒด ์์ ์ผ๋ฐ ํจ์ ์์ ํ์ดํ ํจ์๊ฐ ์๋ ์ผ์ด์ค
const obj = {
name: '๊ผฌ๋ถ๊ธฐ',
sayName() {
console.log(this.name);
const inner = () => {
console.log(this.name);
console.log(this.name === window.name);
}
inner(); //ํ์ดํํจ์๋ฅผ ํธ์ถ์ this๋ ๋ถ๋ชจ ์ค์ฝํ ๊ธฐ์ค
}
}
obj.sayName();
//๊ฐ์ฒด ์์ ์ผ๋ฐ ํจ์ ์์ ์ผ๋ฐ ํจ์๊ฐ ์๋ ์ผ์ด์ค
const obj2 = {
name: '๊ผฌ๋ถ๊ธฐ2',
sayName2() {
console.log(this.name);
function inner() {
console.log(this.name);
console.log(this.name === window.name);
}
inner(); //์ด ํจ์๋ฅผ ํธ์ถํ ๋ this๋ฅผ ๋ฐ๊พธ๋ ๋์์ด ์๋ฌด ๊ฒ๋ ์์ผ๋ฏ๋ก this๋ window
debugger;
}
}
obj2.sayName2();
๊ผฌ๋ถ๊ธฐ
๊ผฌ๋ถ๊ธฐ
false
๊ผฌ๋ถ๊ธฐ2
true
const person = {
name: "ํ๊ธธ๋",
sayName: () => {
console.log(this.name);
const inner = () => {
console.log(this.name);
console.log(this.name === window.name);
};
inner();
},
};
//ํ์ดํ ํจ์์ผ ๊ฒฝ์ฐ this๋ window.
person.sayName(); //''
console.log(window.name); //''
const obj = {
name: '๊ผฌ๋ถ๊ธฐ',
sayName() {
console.log(this.name);
const inner = () => {
console.log(this.name);
console.log(this.name === window.name);
}
inner(); //ํ์ดํํจ์๋ฅผ ํธ์ถ์ this๋ ๋ถ๋ชจ ์ค์ฝํ ๊ธฐ์ค
}
}
obj.sayName();
const obj2 = {
name: '๊ผฌ๋ถ๊ธฐ2',
sayName2() {
console.log(this.name);
function inner() {
console.log(this.name);
console.log(this.name === window.name);
}
inner(); //์ด ํจ์๋ฅผ ํธ์ถํ ๋ this๋ฅผ ๋ฐ๊พธ๋ ๋์์ด ์๋ฌด ๊ฒ๋ ์์ผ๋ฏ๋ก this๋ window
debugger;
}
}
obj2.sayName2();
true
๊ผฌ๋ถ๊ธฐ
๊ผฌ๋ถ๊ธฐ
false
๊ผฌ๋ถ๊ธฐ2
true
const obj = {
name: '๊ผฌ๋ถ๊ธฐ',
sayName: () => {
console.log(this.name);
function inner() {
console.log(this.name);
console.log(this.name === window.name);
}
inner(); //ํ์ดํํจ์๋ฅผ ํธ์ถ์ this๋ ๋ถ๋ชจ ์ค์ฝํ ๊ธฐ์ค
}
}
obj.sayName();
true
<์์ฝ>
this ํจ์ ์ ์ธ์ด ์๋ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ฏ๋ก, ํจ์๊ฐ ํธ์ถ๋ ๋ ๋ง๋ค ๋ค๋ฅผ ์ ์๋ค.
๊ทธ๋ฌ๋ ํ์ดํ ํจ์์ this๋ ์ ์ธ์์ ์ด๊ธฐ ๋๋ฌธ์ ํธ์ถ๋ ๋๋ง๋ค ๋ค๋ฅผ ์๊ฐ ์๋ค.
๊ธฐ๋ณธ this๋ window๋ค.
๋ฐ์ธ๋ฉ: this์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ this๊ฐ ํน์ '๊ฐ์ฒด'์ ์ฐ๊ฒฐ๋๋ ๊ฒ.
this๋ ํจ์๊ฐ ํธ์ถ๋ ๋๋ง๋ค ๋ฌ๋ผ์ง๋ค(๋ ์์ปฌ ์ค์ฝํ์ฒ๋ผ ํจ์๋ฅผ ์ ์ธํ๋ ์๊ฐ์ด ์๋)
๊ธฐ๋ณธ์ window์ด๊ณ , this๋ฅผ ๋ฐ๊ฟ์ฃผ๋ ํ์(ํธ์ถ)์ ํ๋์ง ์ ํ๋์ง๋ฅผ ํ์ธํด์ ํ๋ค.
<this>
๊ธฐ๋ณธthis, globalthis : ์ ์ญ, window
strict mode : undefined
ํ์ดํ ํจ์ : ๋ถ๋ชจ ์ค์ฝํ(์ ์ธ ๊ธฐ์ค)
๊ฐ์ฒด.๋ฉ์๋ : ๊ฐ์ฒด
new ํธ์ถ : ๊ฐ์ฒด
call, apply, bind : this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด ๋ณ๊ฒฝ๊ฐ๋ฅ
2. ํ์ดํ ํจ์
ํ์ดํ ํจ์ : ๋ถ๋ชจ ์ค์ฝํ(์ ์ธ ๊ธฐ์ค)
ํ์ดํ ํจ์์ธ์ง ์๋์ง๋ this๋ฅผ ๊ฒฐ์ ํ๋๋ฐ ์ํฅ์ ๋ผ์น๋ค.
const person = {
name: "ํ๊ธธ๋",
sayName: () => {
console.log(this.name);
},
};
//ํ์ดํ ํจ์์ผ ๊ฒฝ์ฐ this๋ window.
person.sayName(); //''
console.log(window.name); //''
3. ์์ฑ์ ํจ์
์์ฑ์ ํจ์ ๋ด๋ถ์์ this๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค์ ๋ฐ์ธ๋ฉ
new๋ฅผ ๋ถ์ฌ์ ํธ์ถํ๋ ๊ฒฝ์ฐ, this๋ ๊ฐ์ฒด ์๊ธฐ์์
4. this๋ฅผ ๋ฐ๊ฟ์ฃผ๋ ๋ฐฉ๋ฒ bind, apply, call
Call, Apply, Bind ๋ฉ์๋ ์ฌ์ฉ์ ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
function sayName() {
console.log(this.name);
}
sayName(); //window.name์ด๋ฏ๋ก ''
sayName.bind({ name: "ํฉ์ง์ด" })(); //ํฉ์ง์ด
sayName.apply({ name: "๋๊บผ๋น" });//๋๊บผ๋น
sayName.call({ name: "ํ๊ด์ค๋ฆฌ" }); //ํ๊ด์ค๋ฆฌ
- bind : ์๋ก์ด ํจ์๋ฅผ ๋ง๋ฌ, ๊ณ ๋ก ํจ์ ํธ์ถ์ ๊ฐ์ด ํด์ฃผ์ด์ผ ํจ
sayName.bind({ name: "ํฉ์ง์ด" })(); //ํฉ์ง์ด : this๋ฅผ { name: "ํฉ์ง์ด" } ์ผ๋ก ๋ฐ๊พธ๋ ๊ฒ์ด๋ค.
- apply : bind์์ ์คํ๋ถ()๋ง ์ ์ธํ๋ฉด ๋จ. ํจ์ ํธ์ถ์ ์๋์ผ๋ก ํด์ฃผ๊ธฐ ๋๋ฌธ
sayName.apply({ name: "๋๊บผ๋น" });//๋๊บผ๋น : this๋ฅผ { name: "๋๊บผ๋น" } ์ผ๋ก ๋ฐ๊พธ๋ ๊ฒ์ด๋ค.
- call
sayName.call({ name: "ํ๊ด์ค๋ฆฌ" }); //ํ๊ด์ค๋ฆฌ : this๋ฅผ { name: "ํ๊ด์ค๋ฆฌ" }์ผ๋ก ๋ฐ๊พธ๋ ๊ฒ์ด๋ค.
์ฝ๋
1. ์ผ๋ฐ ํจ์ ํธ์ถ์ this๋ ์ ์ญ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
console.log(this === window); // true
a = 30;
console.log(window.a); // 30
function x(){
return this;
}
x() === window; // true
2. ๋ฉ์๋ ํธ์ถ์ this๋ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
let john = {
firstName: "John",
lastName: "Doe",
driveCar() {
console.log(this.firstName + " drives a car.")
}
}
john.driveCar(); // John drives a car
3. ์์ฑ์ ํจ์๋ก ํธ์ถ์ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
function person() {
this.firstName = "John",
this.lastName = "Doe",
this.start = function() {
console.log(this.firstName + "drives a car.")
}
}
let person1 = new person();
console.log(person1); // { firstName: 'John', lastName: 'Doe', start: [Function] }โ
4. Call, Apply, Bind ๋ฉ์๋ ์ฌ์ฉ์ ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
- call
function print( a, b, c ){
console.log(this.name);
console.log(a + b + c);
}
const person = {
name: 'ํ๊ธธ๋'
};
print.call(person,1,2,3);
//ํ๊ธธ๋
//6
call ์ฌ์ฉ์ ํจ์๋ฅผ ์คํํ๊ณ ํจ์์ ์ฒซ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฐ์ฒด์ this๋ฅผ ๋ฐ์ธ๋ฉ
- apply
function print( a, b, c ){
console.log(this.name);
console.log(a + b + c);
}
const person = {
name: 'ํ๊ธธ๋'
};
print.apply(person,[1,2,3] );
//ํ๊ธธ๋
//6
apply๋ call๊ณผ ๊ฑฐ์ ๊ฐ์ ์ฐ์์๋ฅผ ๋ณด์ด์ง๋ง ์ธ์๋ฅผ ๋ฐฐ์ด ํํ๋ก ์ ๋ฌ.
- bind
function logAge ( a, b, c ){
console.log(this.age);
console.log(a + b + c);
}
const person = {
age: 5
};
const myAge = logAge.bind(person);
myAge(1,2,3);
//5
//6
bind์ ๊ฒฝ์ฐ๋ ์ฒซ๋ฒ์งธ์ธ์์ this๋ฅผ ๋ฐ์ธ๋ฉ ํ๋ค๋ ์ ์ ๊ฐ์ง๋ง, ํจ์๋ฅผ ์คํํ์ง ์๊ณ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ค.
์ฆ, ๋ฐํ๋ ์๋ก์ด ํจ์๋ฅผ ์คํํด์ผ ์๋ณธ ํจ์๊ฐ ์คํ๋๋ค.
์์ฝ์ ๋ฆฌ
์ด์ฒ๋ผ this๋ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ .
ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก ํธ์ถํ ๊ฒฝ์ฐ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ,
๋ฉ์๋๋ก ํธ์ถํ ๊ฒฝ์ฐ ์ด๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ,
์์ฑ์ ํจ์๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ
Call, Apply, Bind ๋ฉ์๋ ์ฌ์ฉ์ ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ
์ฐธ๊ณ ๋ฌธํ:
[JS] ์๋ฐ์คํฌ๋ฆฝํธ์์์ this
์๋ฐ์คํฌ๋ฆฝํธ this ์ ๊ฐ๋ ๊ณผ ๋ฐ์ธ๋ฉ ์ค๋ช
Global scope - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
'Javascript > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ์คํฌ๋ฆฝํธ] ๋งค๊ฐ๋ณ์, ์ธ์ ์ฉ์ด ์ฐจ์ด (0) | 2022.05.27 |
---|---|
[์๋ฐ์คํฌ๋ฆฝํธ] ์ต๋ช ํจ์, ํ์ดํ ํจ์, ์ฝ๋ฐฑํจ์ (0) | 2022.05.22 |
[์๋ฐ์คํฌ๋ฆฝํธ] export default (0) | 2022.05.17 |
[์๋ฐ์คํฌ๋ฆฝํธ] ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐฌ์ฑ (0) | 2022.05.10 |
[์๋ฐ์คํฌ๋ฆฝํธ] module ์ฌ์ฉ๋ฒ 3๊ฐ์ง (0) | 2022.05.09 |