๐ Object ์์ฑ์ ํจ์
new ์ฐ์ฐ์๋ก Object ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๋ฐํํ๊ณ ,
์์ฑ๋ ๋น ๊ฐ์ฒด์ ํ๋กํผํฐ ๋๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑ์ํฌ ์ ์๋ค.
//๋น ๊ฐ์ฒด์ ์์ฑ
const person = new Object();
//ํ๋กํผํฐ ์ถ๊ฐ
person.name = 'daniel';
person.age = 29;
person.introduce = function(){
console.log(`์ ๋ ${person.name}์ด๊ณ , ์ ๋์ด๋ ${person.age}์ด ์
๋๋ค.`)
}
console.log(person); // {name: 'daniel', age: 29, introduce: ฦ}
person.introduce(); // ์ ๋ daniel์ด๊ณ , ์ ๋์ด๋ 29์ด ์
๋๋ค.
์์ฑ์ ํจ์๋ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์๋ฅผ ๋งํ๊ณ , ์์ฑ์ ํจ์์ ์ํด ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ธ์คํด์ค๋ผ ํ๋ค.
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ์์๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ์์ด ์๊ณ , ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ ์ด์ ๊ฐ ๊ตณ์ด ์์ด ๋ณด์ธ๋ค.
์ ์์ฑ์ ํจ์๋ฅผ ์จ์ผํ๋ ๊ฒ์ผ๊น?
๐ ์์ฑ์ ํจ์
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ํ ๊ฐ์ฒด ์์ฑ ๋ฐฉ์์ ๋ฌธ์ ์
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ํ ๊ฐ์ฒด ์์ฑ ๋ฐฉ์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ง์ ์์ฑํ๋ค.
๊ทธ๋ ๊ธฐ์ ํ๋กํผํฐ ๊ตฌ์กฐ๊ฐ ๋์ผํจ์๋ ๋ถ๊ตฌํ๊ณ ๊ฐ์ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋๋ง๋ค ๋งค๋ฒ ๊ธฐ์ ํด์ผํ๋ค๋ ์ทจ์ฝํ ๋ฌธ์ ์ ์ ์ง๋๋ค.
์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ ๋ฐฉ์์ ์ฅ์
ํ ํ๋ฆฟ ์ฒ๋ผ ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ ๊ตฌ์กฐ๊ฐ ๋์ผํ ๊ฐ์ฒด ์ฌ๋ฌ๊ฐ๋ฅผ ๊ฐํธํ๊ฒ ์์ฑํ ์ ์๋ค.
function Person(name, age){
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
}
const noah = new Person('noah', 100);
const daniel = new Person('daniel', 29);
console.log(noah); // Person {name: 'noah', age: 100, introduce: ฦ}
console.log(daniel); // Person {name: 'daniel', age: 29, introduce: ฦ}
this?
this๋ ๊ฐ์ฒด ์์ ์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅด๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํ ์๊ธฐ ์ฐธ์กฐ ๋ณ์๋ค. this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ, ์ฆ this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
ํจ์ ํธ์ถ ๋ฐฉ์ this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ(this ๋ฐ์ธ๋ฉ)
์ผ๋ฐ ํจ์๋ก์ ํธ์ถ ์ ์ญ ๊ฐ์ฒด
๋ฉ์๋๋ก์ ํธ์ถ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด(๋ง์นจํ ์์ ๊ฐ์ฒด)
์์ฑ์ ํจ์๋ก์ ํธ์ถ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค
์์ฑ์ ํจ์๋ ์ผ๋ฐ ํจ์์ ๋์ผํ๊ฒ ์ ์ํ๊ณ , new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ๋ฉด ํด๋น ํจ์๋ ์์ฑ์ ํจ์๋ก ๋์ํ๋๋ฐ new ์ฐ์ฐ์์ ํจ๊ป ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ์ง ์์ผ๋ฉด ์์ฑ์ ํจ์๊ฐ ์๋๋ผ ์ผ๋ฐ ํจ์๋ก ๋์ํ๋ค.
function Person(name, age){
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
}
// new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ง ์์ผ๋ฉด ์์ฑ์ ํจ์๋ก ๋์ํ์ง ์์.
// ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋จ.
const noah = Person('noah', 100);
// ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋ Person์ ๋ฐํ๋ฌธ์ด ์์ผ๋ฏ๋ก ์๋ฌต์ ์ผ๋ก undefined๋ฅผ ๋ฐํํ๋ค.
console.log(noah); // undefined
// ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋ Person ๋ด์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
name // 'noah'
age // 100
introduce() // ์ ๋ noah์ด๊ณ , ์ ๋์ด๋ 100์ด ์
๋๋ค.
์์ฑ์ ํจ์์ ์ธ์คํด์ค ์์ฑ ๊ณผ์
์์ฑ์ ํจ์์ ์ญํ
- ์ธ์คํด์ค๋ฅผ ์์ฑ(ํ์)
- ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํ(์ธ์คํด์ค ํ๋กํผํฐ ์ถ๊ฐ ๋ฐ ์ด๊ธฐ๊ฐ ํ ๋น)(์ต์ )
// ์์ฑ์ ํจ์
function Person(name, age){
// ์ธ์คํด์ค ์ด๊ธฐํ
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
}
// ์ธ์คํด์ค ์์ฑ
const daniel = new Person('daniel', 29); //์ด๋ฆ์ด ๋ค๋์์ด๊ณ , ๋์ด๊ฐ 29์ธ Person๊ฐ์ฒด๋ฅผ ์์ฑ
์์ฑ์ ํจ์์๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ๋ฐํํ๋ ์ฝ๋๋ ๋ณด์ด์ง ์๋๋ฐ, ์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ด ๋ค์๊ณผ ๊ฐ์ ๊ณผ์ ์ผ๋ก ์๋ฌต์ ์ผ๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ ํ ์๋ฌต์ ์ผ๋ก ์ธ์คํด์ค๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ธ์คํด์ค ์์ฑ๊ณผ this ๋ฐ์ธ๋ฉ
์๋ฌต์ ์ผ๋ก ๋น ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ฉฐ ์ด ๋น ๊ฐ์ฒด๊ฐ ๋ฏธ์์ฑ์ด์ง๋ง ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ๋น ๊ฐ์ฒด, ์ฆ ์ธ์คํด์ค๊ฐ this์ ๋ฐ์ธ๋ฉ๋๋ค. ์์ฑ์ ํจ์ ๋ด๋ถ์ this๊ฐ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์ด์ ๊ฐ ์ด๊ฒ์ด๋ค. ์ด ์ฒ๋ฆฌ๋ ํ์ ๋ชธ์ฒด์ ์ฝ๋๊ฐ ํ์ค์ฉ ์คํ๋๋ ๋ฐํ์ ์ด์ ์ ์คํ๋๋ค.
function Person(name, age){
console.log(this)// Person {}
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
}
const daniel = new Person('daniel', 29);
์ธ์คํด์ค ์ด๊ธฐํ
์์ฑ์ ํจ์์ ๊ธฐ์ ๋ ์ฝ๋๊ฐ ์คํ๋๋ฉฐ this์ ๋ฐ์ธ๋ฉ๋์ด ์๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ๋ค.
this์ ๋ฐ์ธ๋ฉ๋์ด ์๋ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๊ณ ์์ฑ์ ํจ์๊ฐ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ์ด๊ธฐ๊ฐ์ ์ธ์คํด์ค ํ๋กํผํฐ์ ํ ๋นํ์ฌ ์ด๊ธฐํํ๊ฑฐ๋ ๊ณ ์ ๊ฐ์ ํ ๋นํ๋ค.
์ธ์คํด์ค ๋ฐํ
์์ฑ์ ํจ์ ๋ด๋ถ์ ๋ชจ๋ ์ฒ๋ฆฌ๊ฐ ๋๋๋ฉด ์์ฑ๋ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ๋ this๊ฐ ์๋ฌต์ ์ผ๋ก ๋ฐํ๋๋ค.
function Person(name, age){
// ์๋ฌต์ ์ผ๋ก ๋น ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ this์ ๋ฐ์ธ๋ฉ๋จ.
// this์ ๋ฐ์ธ๋ฉ๋์ด ์๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํ
console.log(this)// Person {}
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
// ์์ฑ๋ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ๋ this๊ฐ ๋ฐํ๋๋ค.
}
// ์ธ์คํด์ค ์์ฑ. Person ์์ฑ์ ํจ์๋ ์๋ฌต์ ์ผ๋ก this๋ฅผ ๋ฐํํ๋ค.
const daniel = new Person('daniel', 29);
๋ง์ฝ this๊ฐ ์๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ช ์์ ์ผ๋ก ๋ฐํํ๋ฉด this๊ฐ ์๋ return๋ฌธ์ ๋ช ์ํ ๊ฐ์ฒด๊ฐ ๋ฐํ๋๋ฉฐ, ์์ ๊ฐ์ ๋ฐํํ๋ฉด ์์ ๊ฐ์ ๋ฌด์๋๊ณ ์๋ฌต์ ์ผ๋ก this๊ฐ ๋ฐํ๋๋ค.
์์ฑ์ ํจ์์์ ๋ช ์์ ์ผ๋ก this๊ฐ ์๋ ๋ค๋ฅธ ๊ฐ์ ๋ฐํํ๋ ๊ฒ์ ์์ฑ์ ํจ์์ ๊ธฐ๋ณธ ๋์์ ํผ์ํ๋ฏ๋ก ์์ฑ์ ํจ์ ๋ด๋ถ์์ return๋ฌธ์ ์๋ตํด์ผ ํ๋ค.
๋ด๋ถ ๋ฉ์๋ [[call]]๊ณผ [[contruct]]
ํจ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ์ผ๋ฐ ๊ฐ์ฒด์ ๋์ผํ๊ฒ ๋์ํ ์ ์๋ค. ํ์ง๋ง ์ผ๋ฐ ๊ฐ์ฒด๋ ํธ์ถํ ์ ์๋ ๋ฐ๋ฉด ํจ์๋ ํธ์ถ์ ํ ์ ์๋ค.
ํจ์ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ ๋ด๋ถ ์ฌ๋กฏ๊ณผ ๋ด๋ถ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- ์ผ๋ฐ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๋ ๋ด๋ถ ์ฌ๋กฏ ๋ฐ ๋ฉ์๋
- [[Environment]]
- [[FormalParameters]]
- [[Call]] : ๋ด๋ถ ๋ฉ์๋, ํจ์๊ฐ ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋๋ฉด ํธ์ถ๋จ.
- [[Construct]] : ๋ด๋ถ ๋ฉ์๋, ํจ์๊ฐ ์์ฑ์ ํจ์๋ก์ ํธ์ถ๋๋ฉด ํธ์ถ๋จ.
๋ด๋ถ ๋ฉ์๋ [[call]]์ ๊ฐ๋ ํจ์ ๊ฐ์ฒด๋ฅผ callable์ด๋ผ๊ณ ํ๋ฉฐ ์ด๋ ํธ์ถํ ์ ์๋ ๊ฐ์ฒด, ์ฆ ํจ์ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
[[constructor]]๋ฅผ ๊ฐ๋ ํจ์ ๊ฐ์ฒด๋ constructor, [[constructor]]๋ฅผ ๊ฐ์ง ์๋ ํจ์ ๊ฐ์ฒด๋ฅผ non-constructor๋ผ๊ณ ํ๋ค.
๋ชจ๋ ํจ์ ๊ฐ์ฒด๋ ํธ์ถ์ ํ ์ ์๊ธฐ์ [[call]]์ ๊ฐ์ง๋ง ๋ฐ๋์ [[constructor]]๋ฅผ ๊ฐ์ง์ง๋ ์๋๋ค.
constructor์ non-constructor์ ๊ตฌ๋ถ
์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ํจ์ ์ ์๋ฅผ ํ๊ฐํ์ฌ ํจ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํจ์ ์ ์ ๋ฐฉ์์ ๋ฐ๋ผ
ํจ์๋ฅผ constructor, non-contructor๋ก ๊ตฌ๋ถํ๋ค.
- constructor: ํจ์ ์ ์ธ๋ฌธ, ํจ์ ํํ์, ํด๋์ค
- non-constructor: ๋ฉ์๋(es6์์ ๋ฉ์๋ ์ถ์ฝ ํํ), ํ์ดํ ํจ์
ECMAScript ์ฌ์์์ ๋ฉ์๋๋ก ์ธ์ ํ๋ ๋ฒ์๊ฐ ์ผ๋ฐ์ ์ธ ์๋ฏธ์ ๋ฉ์๋๋ณด๋ค ์ข๋ค.
์ฌ๊ธฐ์ ๋ฉ์๋๋ ES6์ ๋ฉ์๋ ์ถ์ฝ ํํ๋ง์ ์๋ฏธํ๋ค.
์๋์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์กฐ๊ธ ๋ ์ดํด๊ฐ ์ฝ๊ฒ ๋ค.
function foo(){};
const bar = function() {};
const baz = {
x: function(){}
};
new foo(); // foo {}
new bar(); // bar {}
new baz.x(); // x {}
const arrow = () => {};
new arrow(); // Uncaught TypeError: arrow is not a constructor
const obj = {
x(){}
}
new obj.x(); // Uncaught TypeError: obj.x is not a constructor
new ์ฐ์ฐ์
์์ฑ์ ํจ์๋ก์ ์ ์ํ์ง ์์ ์ผ๋ฐ ํจ์๋ฅผ new ํค์๋๋ก ํธ์ถํ๋ฉด [[construct]]๊ฐ ํธ์ถ๋๊ณ ,
ํจ์๊ฐ ๊ฐ์ฒด๋ฅผ ๋ฐํํ์ง ์์ ์ ๋น๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
๋ฐ๋๋ก ์์ฑ์ ํจ์๋ฅผ new ์ฐ์ฐ์ ์์ด ํธ์ถํ๊ฒ ๋๋ฉด [[call]]์ด ํธ์ถ๋๊ณ ,
์์ฑ์ ํจ์์์ ์๋ this๋ window๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ฉฐ ์์ฑ์ ํจ์์ ์ ์๋ ํ๋กํผํฐ์ ๋ฉ์๋๋ ์ ์ญ์ ํ๋กํผํฐ์ ๋ฉ์๋๊ฐ ๋๋ค.
new.target
new ์ฐ์ฐ์์ ํจ๊ป ์์ฑ์ ํจ์๋ก์ ํธ์ถ๋๋ฉด ํจ์ ๋ด๋ถ์ new.target์ ํจ์ ์์ ์ ๊ฐ๋ฆฌํจ๋ค. new ์ฐ์ฐ์ ์์ด ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋ ํจ์ ๋ด๋ถ์ new.target์ undefined๋ค. ์ด๋ฅผ ์ด์ฉํ์ฌ ์์ฑ์ ํจ์๊ฐ new์์ด ํธ์ถ๋์์ ๋ new ์ฐ์ฐ์์ ํจ๊ป ์ฌ๊ท ํธ์ถ์ ํตํด ์์ฑ์ ํจ์๋ก์ ํธ์ถ์ ํ ์ ์๋ค.
function Person(name, age){
this.name = name;
this.age = age;
this.introduce = function(){
return console.log(`์ ๋ ${this.name}์ด๊ณ , ์ ๋์ด๋ ${this.age}์ด ์
๋๋ค.`)
}
if(!new.target){
return new Person(name, age);
}
}
const daniel = Person('daniel', 29);
์ถ์ฒ : ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฅ๋ค์ด๋ธ
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Webpack (0) | 2021.12.11 |
---|---|
[JavaScript] ํจ์์ ์ผ๊ธ ๊ฐ์ฒด (0) | 2021.11.28 |
[JavaScript] ๋นํธ์ธ ๊ฐ์ฒด (0) | 2021.11.05 |
[JavaScript] Symbol (0) | 2021.10.23 |
[JavaScript]์ ์ญ ๋ณ์์ ๋ฌธ์ ์ (0) | 2021.10.06 |