์ผ๋ฐ์ ์ธ ํจ์ ํธ์ถ ๋ฐฉ๋ฒ
ํจ์();
this ๋ window ๊ฐ์ฒด
function f1(){
console.log(this);
this.alert();
window.alert();
alert();
};
f1();
const f2 = function(){
console.log(this);
};
f2();
๋ฉ์๋๋ก ํธ์ถ
๊ฐ์ฒด์ ์ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋
๊ฐ์ฒด ๋ฉ์๋๋ช
this ๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๊ฐ์ฒด
window.name = "global"; // window ๋ธ๋ผ์ฐ์ ๊ฐ ๊ฐ์ง๊ณ ์๋ ์ ์ญ ๊ฐ์ฒด
global.name = "global"; // node.js๊ฐ ๊ฐ์ง๊ณ ์๋ ์ ์ญ ๊ฐ์ฒด
globalThis.name = "global"; // ๋ธ๋ผ์ฐ์ ๋ฉด window๋ก ์ธ์ํ๊ณ node.js๋ global๋ก ์ธ์ํจ
ํ์ดํ ํจ์ ํธ์ถ
arguments ๋ this ๊ฐ ์์ฑ๋์ง ์๊ณ ์์ ์ปจํ
์คํธ์ arguments, this ๋ฅผ ์ฌ์ฉํ๊ฒ ๋จconst getPingName = () => {
console.log(this);
return this.name;
};
const ping1 = { name: '๋ฐ๋กํ', age: 11, getName: getPingName };
const ping2 = { name: '๋ผ๋ผํ', age: 9, getName: getPingName };
console.log(baro.age, baro.getName());
console.log(rara.age, rara.getName());
์ฒซ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋๋ ๊ฐ์ฒดex02-05.js
function add(x, y) {
console.log(this);
return x + y;
}
console.log(add.apply({ name: "apply" }, [100, 10])); // this๋ {name: "apply"}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ {name: "apply"}
// x= 100, y=10 => x + y = 100 + 10 = 110
ex02-15.js
globalThis.name = "global"; // ๋ธ๋ผ์ฐ์ ๋ฉด window๋ก ์ธ์ํ๊ณ node.js๋ global๋ก ์ธ์ํจ
const getPingName = function () {
return this.name;
};
const baro = new Object();
baro.name = "๋ฐ๋กํ";
baro.age = 9;
baro.getName = getPingName;
const rara = {
name: "๋ผ๋ผํ",
age: 8,
getName: getPingName,
};
// ํจ์ ํธ์ถํ๋ ๋ฐฉ๋ฒ 1 - ์ผ๋ฐ ํจ์๋ก ํธ์ถ(this๋ window)
console.log(getPingName());
// ํจ์ ํธ์ถํ๋ ๋ฐฉ๋ฒ 2 - ๋ฉ์๋๋ก ํธ์ถ(this๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๊ฐ์ฒด, this๋ baro )
console.log(baro.age, baro.getName()); // getName์ this๋ baro
console.log(rara.age, rara.getName()); // getName์ this๋ rara
console.log("=========================================");
console.log(baro.getName(), baro.getName.apply(rara));
// this๋ ๊ฐ๊ฐ baro, rara
p1, p2, p3, โฆ)this ๋ก ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํจ์์ ์ ๋ฌํ ์ธ์๊ฐ์ ์ฐจ๋ก๋๋ก์ง์ ex02-05.js
function add(x, y) {
console.log(this);
return x + y;
}
console.log(add.call({ name: "call" }, 60, 10)); // this๋ {name: "call"}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ {name: call}
// x= 60, y=10 => x + y = 60 + 10 = 70
ex02-15.js
globalThis.name = "global"; // ๋ธ๋ผ์ฐ์ ๋ฉด window๋ก ์ธ์ํ๊ณ node.js๋ global๋ก ์ธ์ํจ
const getPingName = function () {
return this.name; // ํธ์ถํ ๊ฐ์ฒด์ name ์์ฑ์ ๋ฐํํจ
};
const baro = new Object(); // ๋ฐ๋ก๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ์๋ก์ด ๊ฐ์ฒด ์์ฑ
baro.name = "๋ฐ๋กํ"; // name ์์ฑ ์ค์
baro.age = 9; // age ์์ฑ ์ค์
baro.getName = getPingName; // getPingName ํจ์๋ฅผ ๋ฉ์๋๋ก ํ ๋น
const rara = { // rara๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ฆ
name: "๋ผ๋ผํ", // ์ด๋ฆ
age: 8, // ๋์ด
getName: getPingName, // ๋์ ์ด๋ฆ
};
// ํจ์ ํธ์ถํ๋ ๋ฐฉ๋ฒ 1 - ์ผ๋ฐ ํจ์๋ก ํธ์ถ(this๋ window)
console.log(getPingName()); // this.name => global
// ํจ์ ํธ์ถํ๋ ๋ฐฉ๋ฒ 2 - ๋ฉ์๋๋ก ํธ์ถ(this๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๊ฐ์ฒด, this๋ baro )
console.log(baro.age, baro.getName()); // getName์ this๋ baro
console.log(rara.age, rara.getName()); // getName์ this๋ rara
// call์ ์ฌ์ฉํ์ฌ this๋ฅผ ๋ช
์์ ์ผ๋ก ์ง์
console.log("=========================================");
console.log(getPingName(), getPingName.call(baro), getPingName.call(rara));
// this๋ window, baro, rara
push() ๋ฉ์๋ ๊ธฐ๋ฅthis๋ก ์ง์ ๋ Array ๊ฐ์ฒด์ length ์์ฑ๊ฐ์ ํด๋นํ๋ ์์ฑ์ ๋ง๋ค๊ณ ์ง์ ํ ์์๋ฅผ ์ ์ฅํ ํ length๋ฅผ ํ๋ ์ฆ๊ฐ์ํจ๋ค.push() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด์ฒ๋ผ ๋์์ํค๊ธฐlength ์์ฑ ์ถ๊ฐ๊ฐ์ฒด, ์ถ๊ฐํ ์์) โ ex01-28.js์์ฑ์ ํจ์ (๊ฐ์ฒด์งํฅ ์ธ์ด์ ํด๋์ค์ ๋น์ท) ํธ์ถ
new ํจ์๋ช
()this๋ ์์ฑ์๋ฅผ ํตํด ์์ฑ๋ ๊ฐ์ฒด์์ฑ์๋ก ํธ์ถ๋ ๋์ ๋ด๋ถ ๋์ (ex02-19.js)
/*
ํจ์ ํธ์ถ ๋ฐฉ๋ฒ 4 ์์ฑ์ ํจ์
*/
globalThis.name = "global"; // ๋ธ๋ผ์ฐ์ ๋ฉด window๋ก ์ธ์ํ๊ณ node.js๋ global๋ก ์ธ์ํจ
const getPingName = function () {
return this.name;
// ํ์ดํ ํจ์๋ฅผ ๋ง๋ค๋ฉด this๋ฅผ ์์ฑํ์ง ์์
};
// ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํํ๋ ํจ์(์์ฑ์)
function Ping(name, age) {
this.name = name;
this.age = age;
this.getName = function () {
return this.name;
};
}
/**
* new์ ๋์
* 1. ๋น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ this ์์ฑ
* 2. this๋ฅผ ์์ฑ์ ํจ์์ ์ ๋ฌ
* 3. ์์ฑ์ ํจ์๊ฐ ์๋ฌด๊ฒ๋ ๋ฆฌํดํ์ง ์๋๋ค๋ฉด this๋ฅผ ์๋์ผ๋ก ๋ฆฌํด
*/
const baro = new Ping("๋ฐ๋กํ", 7);
const rara = new Ping("๋ผ๋ผํ", 8);
const copyPing = new Ping("์นดํผํ", 12);
baro.age++;
rara.age++;
console.log("์ด ํ์ ์ด๋ฆ์:", baro.getName(), "| ๋์ด๋:", baro.age); // getName์ this๋ baro
console.log("์ด ํ์ ์ด๋ฆ์:", rara.getName(), "| ๋์ด๋:", rara.age); // getName์ this๋ rara
console.log("์ด ํ์ ์ด๋ฆ์:", copyPing.getName(), "| ๋์ด๋:", copyPing.age); // getName์ this๋ copyPing
const baro = new Ping("๋ฐ๋กํ", 7);
new Ping(...)์ ํธ์ถํ๋ฉด, ์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ด๋ถ์ ์ผ๋ก
this = {}์ ๊ฐ์ ๋น์ด ์๋ ๊ฐ์ฒด๋ฅผ ๋จผ์ ์์ฑํ๋ค.
this ๋งค๊ฐ๋ณ์๋ก ์์ฑ์ ํจ์์ ์ ๋ฌfunction Ping(name, age) {
this.name = name;
this.age = age;
this.getName = function () {
return this.name;
};
}
์์์ ๋ง๋ค์ด์ง this = {}๋
Pingํจ์ ์์ผ๋กthis๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ค์ด๊ฐ๊ทธ๋ฐ ๋ค์
this.name,this.age๊ฐ์ ์์ฑ๋ค์ด ์ฑ์์ง
// Ping ํจ์์๋ return์ด ์์
Ping ํจ์๋ ๋ช ์์ ์ผ๋ก return์ ํ์ง ์์ โ
๊ทธ๋์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์๋์ผ๋ก ์ฑ์์ง
this๊ฐ์ฒด๋ฅผ ๋ฆฌํดํจ์ฆ,
baro๋ณ์์๋{ name: "๋ฐ๋กํ", age: 7, getName: function }์ด ์ ์ฅ๋จ
new ์ฐ์ฐ์์ ๋น์ทํ ๋์const baro = new Ping("๋ฐ๋กํ", 7);
์ด ์ ์ฒด ํ๋ฆ์ Java, C++ ๊ฐ์ ๊ฐ์ฒด์งํฅ ์ธ์ด์์์ new์ ์ ์ฌํจ
์ฆ,
new๋ ๊ฐ์ฒด ์์ฑ + ์ด๊ธฐํ + ๋ฐํ๊น์ง ํ ๋ฒ์ ํด์ฃผ๋ ํค์๋์ผ
์์ฑ์๋ฅผ ์์ฑํ ๋ ๊ณ ๋ คํด์ผ ํ ๊ฒ๋ค
this๋ window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก ๊ฐ์ฒด์ ์ข
์์ ์ธ ๊ฐ์ ์ง์ ํ ์ ์์ผ๋ฏ๋ก ์๋ฏธ๊ฐ ์๋ค.