๐ ์์ฑ์ ํจ์๊ณผ ํ๋กํ ํ์
this๋ฅผ ๊ณต๋ถํ๊ณ ๋์ ์์ฑ์ ํจ์, ํ๋กํ ํ์ , ํด๋์ค์ ๋ํด ํ๋ฒ ์ ๋ฆฌํ ํ์์ฑ์ ๋๊ผ๋ค. ์์ฑ์ ํจ์๋ฅผ ์์์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด์งํฅ์ ๋ํด ์ ๋ฆฌํด๋ณด๊ณ ์ ํ๋ค.
โ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ
์์ฑ์ ํจ์๋, ํด๋์ค๋ ๊ฒฐ๊ตญ์๋ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ด๋ค. ๊ทธ๋์ ๊ฐ์ฅ ๋จผ์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์ ๋ฆฌํด ๋ณด๊ณ ์ ํ๋ค.
1) ๊ฐ์ฒด ๋ฆฌํฐ๋ด
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๊ฐ์ฅ ์ฝ๊ฒ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ผ๋ก {}๋ก ๋ด๋ถ์ ๋ฃ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ๋ด์ ๋ง๋ค ์ ์๋ค.
const position = {
x: 1,
y: 2,
}2) ์์ฑ์ ํจ์
์์ฑ์ ํจ์๋ new์ฐ์ฐ์๋ฅผ ์ด์ฉํด ๊ฐ์ฒด, ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ํจ์๋ฅผ ์๋ฏธํ๋ค. ๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ณด๋ค๋ ์๋์ ์ผ๋ก ๋ณต์กํ๊ฒ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ด๋ค.
2-1) Object ์์ฑ์ ํจ์
Object ์์ฑ์ ํจ์์ new ํค์๋๋ฅผ ์ด์ฉํ๋ฉด ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. ์ดํ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ๋ด๋ถ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
const person = new Object()
person.name = `lee`
person.sayHello = function () {
console.log(this.name)
}Object ์์ฑ์ ํจ์ ์ธ์ ๋นํธ์ธ ๊ฐ์ฒด๋ค๋ new์ ํจ๊ป ์์ฑํ ์ ์๋ค.
const strObj = new String("youngjun")
console.log(strObj)
const numObj = new Number(123)
console.log(numObj)
const boolObj = new Boolean(true)
console.log(boolObj)
const func = new Function("x", "return x*x")
console.log(func)
const arr = new Array(1, 2, 3)
console.log(arr)
const regExp = new RegExp(/ab+c/i)
console.log(regExp)2-2) ์ฌ์ฉ์ ์ ์ ์์ฑ์ ํจ์
ํจ์ ๋ด๋ถ์ ์ธ์คํด์ค์ ์ถ๊ฐํ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ ์ํ ํ์ new ํค์๋๋ก ์ธ์คํด์ค๋ฅผ ๋ง๋ค ์ ์๋ค.
function Person(name) {
this.name = name
this.sayHello = function () {
console.log(this.name)
}
}
const person = new Person("youngjun")
console.log(person)3) Object.create()
ํ๋กํ ํ์ ์ ์์์ ์ด์ฉํด ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ผ๋ก ์์ฒด์ ์ผ๋ก๋ ๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง๋ง, prototype์ผ๋ก ์ ๋ฌ ๋ฐ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
const obj1 = { a: 1, b: 2 }
const obj2 = Object.create(obj1)
console.log(obj2) // {}
console.log(obj2.a) // 1๐ ์์ฑ์ ํจ์์ ํ์์ฑ๊ณผ ๋์๋ฐฉ์
๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ๋ค ์ค์ ์ ์์ฑ์ ํจ์๊ฐ ์ฐ์ด๋์ง์ ๋์ ๋ฐฉ์์ ์์๋ณด์.
์์ฑ์ ํจ์์ ํ์์ฑ
์์ ์ธ๊ฐ์ง ๋ฐฉ์ ์ค ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ด ๊ฐ์ฅ ํธํ๊ฒ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๋ ๋ฐฉ๋ฒ์ด๋ค. ๊ฐ์ฒด์ ํ์ํ ๊ฐ์ ์ง์ ์ ์ํด์ค ์ ์์ด ์ปค์คํ ํ๊ธฐ๋ ์ฝ๋ค. ํ์ง๋ง ๋๊ฐ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ๊ฐ์ง๋ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด๊ฐ ํ์ํ๋ค๋ฉด ๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ก ์ผ์ผ์ด ๋ง๋๋ ๊ฒ์ ๋นํจ์จ์ ์ด๋ค.
const person1 = {
name: "youngjun",
sayHello() {
console.log(this.name)
},
}
const person2 = {
name: "minjae",
sayHello() {
console.log(this.name)
},
}์ด๋ ๊ฒ ์ฌ๋ฌ ๊ฐ์ ์ ์ฌํ ๊ฐ์ฒด๊ฐ ํ์ํ ๋, ํ ํ๋ฆฟ์ ๋ง๋ค๊ณ ํ์ํ ๋ถ๋ถ๋ง ์ฃผ์ ๋ฐ์์ ์ฌ์ฉํ๋ฉด ํธํ์ง ์์๊น๋ผ๋ ์๊ฐ์ด ๋ ๋ค. ์ด๋ด ๋ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด ๋ฐ๋ก ์์ฑ์ ํจ์๋ค.
function Person(name) {
this.name = name
this.sayHello = function () {
console.log(this.name)
}
}
const person = new Person("youngjun")
console.log(person)๋์๋ฐฉ์
์์ฑ์ ํจ์๋ฅผ ์ด์ฉํด ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ 1) ์ธ์คํด์ค์์ฑ 2) ์ธ์คํด์ค ์ด๊ธฐํ 3) ์ธ์คํด์ค ๋ฐํ ์ธ ๊ฐ์ง ๊ณผ์ ์ผ๋ก ์งํ๋๋ค. ์์ ์์ ๋ฅผ ๋ค์ ์ดํด๋ณด์.
function Person(name) {
this.name = name
this.sayHello = function () {
console.log(this.name)
}
}
const person = new Person("youngjun")
console.log(person)1) ์ธ์คํด์ค ์์ฑ
์์ฑ์ ํจ์๋ ์๋ฌต์ ์ผ๋ก ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ์์ฑ์ ํจ์์ this์ ๋ฐ์ธ๋ฉ์ด ๋๋ค.
function Person(name) {
// 1) ์ธ์คํด์ค ์์ฑ, this์ ๋ฐ์ธ๋ฉ
this.name = name
this.sayHello = function () {
console.log(this.name)
}
}
const person = new Person("youngjun")
console.log(person)2) ์ธ์คํด์ค ์ด๊ธฐํ
this์ ๋ฐ์ธ๋ฉ ๋์ด์๊ธฐ ๋๋ฌธ์ ์ดํ์ this๋ฅผ ์ด์ฉํด ์ด๊ธฐํ ๊ณผ์ ์ ์งํํ ์ ์๋ค.
function Person(name) {
// 1) ์ธ์คํด์ค ์์ฑ, this์ ๋ฐ์ธ๋ฉ
// 2) ์ธ์คํด์ค ์ด๊ธฐํ
this.name = name
this.sayHello = function () {
console.log(this.name)
}
}
const person = new Person("youngjun")
console.log(person)3) ์ธ์คํด์ค ๋ฐํ
์ด๊ธฐํ ๊ณผ์ ์ด ๋๋๋ฉด return์ผ๋ก ์ ์ง ์์๋ ์๋ฌต์ ์ผ๋ก this๊ฐ ๋ฐํ๋๋ค. ์ด๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ returnํ๋ฉด ๋ช ์ํ ๊ฐ์ฒด๊ฐ ๋ฐํ๋๊ณ , ์์๊ฐ์ผ๋ก ๋ฐํํ๋ฉด ์๋ฌต์ ์ผ๋ก this๊ฐ ๋ฐํ๋๋ค.
function Person(name) {
// 1) ์ธ์คํด์ค ์์ฑ, this์ ๋ฐ์ธ๋ฉ
// 2) ์ธ์คํด์ค ์ด๊ธฐํ
this.name = name
this.sayHello = function () {
console.log(this.name)
}
// 3) ์ธ์คํด์ค ๋ฐํ
}
// ๋ช
์์ ์ผ๋ก ๋ค๋ฅธ ๊ฐ์ฒด ๋ฐํ
function Person(name) {
this.name = name
this.sayHello = function () {
console.log(this.name)
}
return {}
}
const person = new Person("youngjun")
console.log(person) // {}
//๋ช
์์ ์ผ๋ก ์์๊ฐ ๋ฐํ
function Person(name) {
this.name = name
this.sayHello = function () {
console.log(this.name)
}
return "hi"
}
const person = new Person("youngjun")
console.log(person) // Person { name: 'youngjun', sayHello: [Function (anonymous)] }โ ๋ด๋ถ ๋ฉ์๋
์์ฑ์ ํจ์๋ ์ผ๋ฐํจ์์ ๋์ผํ๊ฒ ์์ฑํ์ง๋ง ๋ค๋ฅด๊ฒ ๋์ํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค. ์ด๋ค ์ฐจ์ด๊ฐ ์๋์ง ์์ฑ์ ํจ์์ ๋ด๋ถ ๋์์ ์บ๋ณด์.
ํจ์ ๊ฐ์ฒด์ [[call]]๊ณผ [[Construct]]
ํจ์๋ ๊ฐ์ฒด๋ค. ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๊ธฐ์กด์ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๋ ๊ฐ์ฒด์ ํน์ฑ์ ์ด์ฉํ ์ ์๋ค.
function bar() {}
bar.a = 1
console.log(bar.a) // 1ํ์ง๋ง ํจ์ ๊ฐ์ฒด๋ ์ผ๋ฐ ๊ฐ์ฒด์ ๋ค๋ฅด๊ฒ ํธ์ถํ ์ ์๊ณ ์์ฑ์ ํจ์๋ก๋ ์๋ํ ์ ์๋ค. ๊ทธ์ด์ ๋ ํจ์ ๊ฐ์ฒด์๋ ์ผ๋ฐ ๊ฐ์ฒด์ ๋ด๋ถ ๋ฉ์๋๋ฟ๋ง ์๋๋ผ [[call]]๊ณผ [[construct]]๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๋ ์ค์ํ ๋ถ๋ถ์ ๋ชจ๋ ํจ์๋ [[call]]์ ๊ฐ์ง๊ณ ์์ด ํธ์ถ์ด ๊ฐ๋ฅํ์ง๋ง, ๋ชจ๋ ํจ์๊ฐ [[construct]]๋ฅผ ๊ฐ์ง๋ ๊ฒ์ ์๋๋ค๋ผ๋ ๊ฒ์ด๋ค. ์ด๋ ๊ฒ [[construct]]๋ฅผ ๊ฐ์ง๋ ํจ์๋ฅผ constructor๋ผ๊ณ ๋ถ๋ฅด๊ณ [[construct]]๋ฅผ ๊ฐ์ง์ง ์๋ ํจ์๋ non-constructor๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ๋ค์๊ณผ ๊ฐ์ด ์ ๋ฆฌ๋๋ค.
- constructor: ํจ์ ์ ์ธ๋ฌธ, ํจ์ ํํ์, ํด๋์ค
- non-constructor: ๋ฉ์๋ ์ถ์ฝ, ํ์ดํ ํจ์
์ด๋ ๊ฒ ๋ด๋ถ์ ์ผ๋ก [[construct]]๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด new์ ํจ๊ป ์ฌ์ฉํ๋ฉด ์ธ์ ๋ ์์ฑ์ ํจ์๋ก ์๋ํ ์ ์๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์์ฑ์ ํจ์๋ก ์ฌ์ฉํ์ง ์์ ๊ฑฐ๋ผ๋ฉด ํ์ดํํจ์์ ๊ฐ์ฒด ๋ด๋ถ์์๋ ๋ฉ์๋ ์ถ์ฝ์ ์ฌ์ฉํ๋ ๊ฒ ๋ ์ข๋ค๊ณ ์๊ฐ๋๋ค.
// ์ผ๋ฐํจ์๋ฅผ ์์ฑ์ํจ์๋ก
function add(x, y) {
return x + y
}
let inst = new add()
console.log(inst) //add{}
function createUser(name, role) {
return { name, role }
}
inst = new createUser("lee", "admin")
console.log(inst) // { name: 'lee', role: 'admin' }
//์์ฑ์ํจ์๋ฅผ ์ผ๋ฐํจ์๋ก
function Circle(radius) {
this.radius = radius
this.getDiameter = function () {
return 2 * this.radius
}
}
const circle = Circle(5)
console.log(circle) // undefined
console.log(radius) // 5
console.log(getDiameter()) //10์ ์ฝ๋์์ ์ผ๋ฐํจ์๋ฅผ ์์ฑ์ํจ์๋ก ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ๋ณผ ์ ์๊ณ , ์์ฑ์ ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์์๋ this๊ฐ window๊ฐ ๋์ด ์ ์ญ๊ฐ์ฒด์ ์์ฑ๊ณผ ๋ฉ์๋๋ก ๋ฑ๋ก๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
๐ ์์ฑ์ ํจ์ ๊ตฌ๋ถ
์์ ์ฝ๋์ฒ๋ผ ์ผ๋ฐํจ์์ ์์ฑ์ํจ์๋ ๊ตฌ๋ถํ๊ธฐ ํ๋ค๊ธฐ ๋๋ฌธ์, ์์ฑ์ ํจ์๋ ๋๋ฌธ์๋ก ์์ํ๋ Pascalํ๊ธฐ๋ฒ์ ์ด์ฉํ๋ค. ํ์ง๋ง ๋จ์ํ ๋ช
๋ช
๋ฒ์ผ๋ก ๊ตฌ๋ถํ๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ new.target์ ์ด์ฉํด new๋ฅผ ์ด์ฉํด ์์ฑ์ ํจ์๋ก ํจ์๊ฐ ํธ์ถ๋์๋์ง ํ์ธํ ์ ์๋ค.
new.target์ new์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋๋ฉด ํจ์ ์์ ์ ๊ฐ๋ฆฌํค๊ณ , ์ผ๋ฐํจ์๋ก ํธ์ถ๋์์ ๋๋ undefined๋ก ๋ํ๋๊ธฐ ๋๋ฌธ์ ๊ฐ๊ฐ์ ๋ฐ๋ฅธ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
function Circle(radius) {
if (!new.target) {
return new Circle(radius)
}
this.radius = radius
this.getDiameter = function () {
return 2 * this.radius
}
}
const circle = Circle(5)
console.log(circle.getDiameter())์ ์ฝ๋์์ new ํค์๋ ์์ด ํธ์ถ๋์์ง๋ง new.target์ ์ด์ฉํด ์์ฑ์ํจ์๋ก ๋ค์ ํธ์ถ์์ผ ๊ฐ์ฒด๋ฅผ ๋ง๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
โจ ํ๋กํ ํ์
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ฉํฐํจ๋ฌ๋ค์ ์ธ์ด๋ก ํ๋กํ ํ์
๊ธฐ๋ฐ์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ง์ํ๋ค. ์ฌ๊ธฐ์ ๋จผ์ ํ๋กํ ํ์
๊ธฐ๋ฐ์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ ๋์์ ์ํ๊ณผ ๋์์ ํ๋๋ก ๋ฌถ์ ๊ฐ์ฒด๋ค์ ์งํฉ์ผ๋ก ํ๋ก๊ทธ๋จ์ ํํํ๋ ค๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ ์๋ฏธํ๋ค. ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ์ดํดํ์ผ๋ ๋ค์์ผ๋ก ํ๋กํ ํ์
์ ์ด๋ค ๊ฒ์ธ์ง ์์๋ณด์.
๐โโ๏ธ ํ๋กํ ํ์ ์ด๋
ํ๋กํ ํ์ ์ ์ฐ๋ฆฌ๊ฐ ์์ ์์๋ณธ ์์ฑ์ ํจ์๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ์ ๋, ๋ง๋ค์ด ์ง ๊ฐ์ฒด์ ๋ถ๋ชจ์ ๊ฐ์ด ์์ ๊ฐ์ฒด ์ญํ ์ ํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ์ฆ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์์์ ๊ตฌํํ๋ ๋ฐฉ์์ด๋ค. ๋ง๋ค์ด ์ง ๊ฐ์ฒด๋ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ์์ํ๊ณ ์๊ธฐ ๋๋ฌธ์, ํ๋กํ ํ์ ๊ฐ์ฒด์ ๋ฉ์๋๋ค์ ์ฌ์ฉํ ์ ์๋ค.
๊ทธ๋ฌ๋ฉด ํ๋กํ ํ์ ์ด ์ด๋ป๊ฒ ๋ง๋ค์ด์ง๋ ์ง ์ ๋ฆฌํด๋ณด์
ํจ์ ์ ์ธ
ํจ์์๋ ๋ด๋ถ ์ฌ๋กฏ์[[call]]๊ณผ [[construct]]๊ณผ ๊ฐ์ด [[prototype]]์ด ์กด์ฌํ๋๋ฐ ์ด๋ ํจ์๋ฅผ ์ ์ธํ ๋, ํด๋น ํจ์๋ก ๋ง๋ค์ด ์ง ์ธ์คํด์ค๊ฐ ์์ํ ํ๋กํ ํ์
์ ๊ฐ๋ฆฌํค๊ณ ์๋ค. prototype๊ฐ์ฒด๋ [[construct]]๋ก ์์ฑ์ํจ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ค. ์ด๋ ๊ฒ ํ๋กํ ํ์
๊ณผ ์์ฑ์ํจ์๋ ๋จ๋
์ผ๋ก ์กด์ฌํ๋ ๊ฒ์ด ์๋๋ผ ์๋ก prototype๊ณผ constructor์์ฑ์ผ๋ก ํจ๊ป ์กด์ฌํ๋ค.
function Foo() {}
console.log(Foo.prototype) // {}
console.log(Foo.prototype.constructor) // [Function: Foo]์ธ์คํด์ค ์์ฑ
์์ฑ์ํจ์์ new ํค์๋๋ฅผ ์ด์ฉํด ์ธ์คํด์ค๋ฅผ ๋ง๋ค๋ฉด ์ธ์คํด์ค๋ ํ๋กํ ํ์
์ ๋ฉ์๋๋ฅผ ์์ ๋ฐ์ ์ฌ์ฉํ ์ ์๋ค. ์์ฑ์ํจ์์ ๋ค๋ฅด๊ฒ [[prototype]]์ ์ง์ ์ ๊ทผํ ์๋ ์์ง๋ง __proto__๋ฅผ ์ด์ฉํด์ ๊ฐ์ ์ ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
function Foo() {}
Foo.prototype.val = "hi"
console.log(Foo.prototype) // { val: 'hi' }
console.log(Foo.prototype.constructor) // [Function: Foo]
const obj1 = new Foo()
console.log(obj1) // Foo {}
console.log(obj1.val) // hiobj1์ ์์ฑ์ ํจ์์์ val ์์ฑ์ ์ ์ํ ์ ์ด ์์ง๋ง ํ๋กํ ํ์ ์ ์ ์ํ ๊ฒ์ ์์๋ฐ์ obj1์์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฒ์ ์ ์ ์๋ค.
๐ __proto__์ prototype
์์ ์์ฑ์ ํจ์, ํ๋กํ ํ์
, ์ธ์คํด์ค ์ธ ๊ฐ์ง์ ๊ด๊ณ๋ฅผ ์ดํดํ๊ธฐ ์ํด __proto__์ [[prototype]] ๋ฅผ ์ด์ฉํด ์ค๋ช
ํ๋ค. ๋์ผํ๊ฒ ํ๋กํ ํ์
์ ๊ฐ๋ฆฌํค๋ ๋น ๋ ๊ฐ์ง ์์ฑ์ ํท๊ฐ๋ฆฌ์ง ์๊ฒ ๋จผ์ ์ ๋ฆฌํด ๋ณด๊ณ ์ ํ๋ค.
๋จผ์ __proto__๋ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ํ๋กํ ํ์
์ ์ ๊ทผํ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ค. __proto__ ๋ ๊ฐ์ฒด๊ฐ ์ง์ ๊ฐ์ง๊ณ ์๋ ๊ฒ ์๋๋ผ ํ๋กํ ํ์
์ ์ด์ฉํ ์์์ผ๋ก Object.prototype์ ์ ๊ทผ์ ์์ฑ์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. __proto__๋ฅผ ์ด์ฉํด์ ํ๋กํ ํ์
์ ์ ๊ทผํ ์ ์๊ฒ ํ ์ด์ ๋ ์ํธ ์ฐธ์กฐ์ ์ํด ํ๋กํ ํ์
์ฒด์ธ์ด ์๊ธฐ์ง ์๊ฒ ๋ฐฉ์งํ๊ธฐ ์ํด์๋ค. ์๋ก ์์ํด ์์์ด์ ๋ถ๋ชจ๊ฐ ๋๋ ์ํฉ์ด ๋์ง ์๊ฒ, ๊ฒ์ ๊ณผ์ ์ด ๋ฌดํ ์ํ๊ฐ ๋์ง ์๊ฒ ๋ง์ ์ ์๋ค.
ํ์ง๋ง __proto__ ์์ฑ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ๋ ์๊ธฐ ๋๋ฌธ์ ํ๋กํ ํ์
์ ์ ๊ทผํ ๋ Object.getPrototypeof๋ฅผ, ํ๋กํ ํ์
์ ๊ต์ฒดํ ๋๋ Object.setPrototypeOf๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
const obj = Object.create(null) // ์์์ ์ด์ฉํด ๋ง๋ ๊ฐ์ฒด
console.log(obj.__proto__) // undefined
console.log(Object.getPrototypeOf(obj)) //null
const child = {}
const parent = { x: 1 }
Object.setPrototypeOf(child, parent)
console.log(obj2.x) // 1prototype์ ํจ์๊ฐ ๊ฐ์ง๋ ์์ฑ์ผ๋ก ์์ฑ์ ํจ์๊ฐ ๋ง๋ค ์ธ์คํด์ค์ ํ๋กํ ํ์
์ ๊ฐ์ง๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์์ฑ์ ํจ์๊ฐ ๋์ง ๋ชปํ๋, [[construct]]๊ฐ ์๋ ํ์ดํํจ์๋ ๋ฉ์๋ ์ถ์ฝํํ์ prototype์์ฑ์ ๊ฐ์ง์ง ์๊ณ , ํ๋กํ ํ์
์ ์์ฑํ์ง ์๋ ๊ฒ์ ์ ์ ์๋ค.
const Person = name => {
this.name = name
}
console.log(Person.hasOwnProperty("prototype")) //false๋์ ์ ๋ฆฌํ๋ฉด __proto__๋ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์์ง๋ง prototype์ ์์ฑ์ ํจ์๋ง ๊ฐ์ง๊ณ ์๋ ์ฐจ์ด๋ฅผ ๊ฐ์ง๋ค.
โ ์์ฑ์ ํจ์์ ํ๋กํ ํ์
์์ ์ค๋ช ์ ์์ฑ์ํจ์์ ํ๋กํ ํ์ ์ ํญ์ ํจ๊ป ์์ฑ๋๋ค๊ณ ์ค๋ช ํ๋ค. ์์ฑ์ ํจ์๋ ์ฌ์ฉ์ ์ ์ ์์ฑ์ํจ์์ ๋นํธ์ธ ์์ฑ์ํจ์๋ก ๊ตฌ๋ถํ ์ ์๋ค. ์ด ๋๊ฐ์ง ๋ฐฉ์์์ ์์ฑ์ํจ์๊ฐ ์ธ์ ์์ฑ๋๋์ง ์์๋ณด์.
์ฌ์ฉ์ ์ ์ ์์ฑ์ ํจ์
console.log(Foo.prototype) // {}
function Foo(name) {
this.name = name
}์ ์ฝ๋์์ Fooํจ์๋ ํ๊ฐ ๋จ๊ณ์์ ์ ์ญ ์ค์ฝํ์ ๋ฑ๋ก๋๋ฉด์ ํจ์ ๊ฐ์ฒด๊ฐ ๋ฐ๋ก ์ด๊ธฐํ๊ฐ ๋๋ค. ์ด๋ ํ๋กํ ํ์
๋ ํจ๊ป ์์ฑ๋์ด ๋ฐ์ธ๋ฉ ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Foo. prototype์ ๋จผ์ ํธ์ถํด๋ ์๋ฌ๊ฐ ๋์ง ์๊ณ ๊ฐ์ด ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ค์ํ ๊ฒ์ "๋ชจ๋ ๊ฐ์ฒด๋ ํ๋กํ ํ์
์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์" ๋ฐ์ธ๋ฉ๋ ํ๋กํ ํ์
๋ ์์ ์ ํ๋กํ ํ์
์ Object.Prototype์ผ๋ก ๊ฐ์ง๊ฒ ๋๋ค.
๋นํธ์ธ ์์ฑ์ ํจ์
๋นํธ์ธ ์์ฑ์ํจ์๋ Object, String, Number๋ฑ ๊ณผ ๊ฐ์ ํจ์๋ก ์ ์ญ ๊ฐ์ฒด(๋ธ๋ผ์ฐ์ : Window, NodeJS: Global) ๊ฐ ์์ฑ๋ ๋ ํจ๊ป ์์ฑ๋๋ค. ์ด๋ ์ฌ์ฉ์ ์ ์ ์์ฑ์ ํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์ํจ์๊ฐ ๋ง๋ค์ด์ง ๋ ํจ๊ป ํ๋กํ ํ์
๋ ๋ง๋ค์ด์ ธ ๋นํธ์ธ ์์ฑ์ํจ์์ prototype์ ๋ฐ์ธ๋ฉ๋๋ค.
๐ ๊ฐ์ฒด ์์ฑ ๋ฐฉ์์ ๋ฐ๋ฅธ ํ๋กํ ํ์
์์ฑ์ ํจ์๋ฅผ ์ ๋ฆฌํ๋ฉด์ ๋จผ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ค ๊ฐ์ง ๋ฐฉ์์ ์ค๋ช ํ๋ค. ๋ค ๊ฐ์ง ๋ฐฉ์์ ๋ฐ๋ผ ์ด๋ป๊ฒ ํ๋กํ ํ์ ์ด ์ ํด์ง๋์ง ์์๋ณด์.
1) ๊ฐ์ฒด ๋ฆฌํฐ๋ด
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ Object.Prototype์ ํ๋กํ ํ์ ์ผ๋ก ๊ฐ๋๋ค.
const obj = {}
console.log(obj.constructor === Object)2) Object ์์ฑ์ ํจ์
Object ์์ฑ์ํจ์๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด๊ณผ ๋์ผํ๊ฒ Object.Prototype์ ํ๋กํ ํ์ ์ผ๋ก ๊ฐ๋๋ค.
const Obj = new Object()
console.log(obj.constructor === Object)3) ์์ฑ์ ํจ์์ ์ํด ์์ฑ๋ ๊ฐ์ฒด
์์ฑ์ํจ์๋ ์์ฑ๋ ๋ ํ๋กํ ํ์ ์ด ๋ฐ์ธ๋ฉ ๋์ด ์์ด, ์์ฑ์ ํจ์๋ก ๋ง๋ค์ด์ง๋ ๊ฐ์ฒด๋ ์์ฑ์ํจ์์ prototype ์์ฑ์ ๋ฐ์ธ๋ฉ๋ ํ๋กํ ํ์ ์ ๊ฐ๋๋ค. ์ ๋ ๊ฐ์ง ๋ฐฉ์๊ณผ ๋ค๋ฅธ ์ ์ Object.Prototype์ ๋ค์ํ ๋ฉ์๋๊ฐ ์์ง๋ง ์์ฑ์ํจ์์ ๋ฐ์ธ๋ฉ๋ ํ๋กํ ํ์ ์ constructor ์์ฑ๋ง ๊ฐ์ง๊ณ ์๋ ํน์ง์ ๊ฐ์ง๋ค.
function Person(name) {
this.name = name
}
Person.prototype.sayHello = function () {
console.log(`${this.name}`)
}
const me = new Person("Lee") // Lee
const you = new Person("Kim") // Kim
me.sayHello()
console.log(me.hasOwnProperty) // [Function: hasOwnProperty]me์ you๋ ํ๋กํ ํ์ ์ sayHello๋ฅผ ์์ ๋ฐ์ ์ฌ์ฉํ ์ ์๋ค. ํ์ง๋ง ๋ถ๋ช ๋ฐ์ธ๋ฉ๋ Person.Prototype์๋ ์๋ก ์ถ๊ฐํ sayHello์ constructor๋ง ์์ฑ์ผ๋ก ๊ฐ์ง๊ณ ์์ด์ผํ๋๋ฐ ์ด๋ป๊ฒ Object.Prototype์ hasOwnProperty๋ ์์ ๋ฐ์ ์ ์์๊น?
๐ ํ๋กํ ํ์ ์ฒด์ธ
์์ ์์ ์์ Person.Prototype์๋ constructor๋ง ์์ด์ผ ํ์ง๋ง hasOwnProperty๋ ์ฌ์ฉํ ์ ์๋ค. ์ด์ฒ๋ผ ๊ฐ์ฒด์์ ์ ๊ทผํ๋ ค๋ ์์ฑ์ด ์์ ๋์๋ [[prototype]]์ ์ด์ฉํด, ์์๋ฐ์ ๋ถ๋ชจ์ ํ๋กํผํฐ๋ค์ ์์ฐจ์ ์ผ๋ก ๊ฒ์ํ๋ค. ์ด๋ ๊ฒ ํ๋กํ ํ์ ์ผ๋ก ๊ณ์ํด์ ์ฐ๊ฒฐ๋์ด์๋ ๊ฒ์ ํํ ํ ํ์ ์ฒด์ธ์ด๋ผ๊ณ ํ๋ค.
์ด๋ ๊ฒ ์ฒด์ด๋์ด ๊ฐ๋ฅํ ์ด์ ๋ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ํ๋กํ ํ์ ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ๊ณ์ํด์ ํ๊ณ ์ฌ๋ผ๊ฐ ์ ์๋ค. ํ๋กํ ํ์ ์ฒด์ธ์ ๊ฐ์ฅ ๋์ ํญ์ Object.prototype์ผ๋ก Object.Protype์ ํ๋กํ ํ์ ์ null๋ก ํ์์ด ์ข ๋ฃ๋๋ค.
function Person(name, gender) {
this.name = name
this.gender = gender
this.sayHello = function () {
console.log("Hi! my name is " + this.name)
}
}
const foo = new Person("Lee", "male")
console.log(foo.__proto__ === Person.prototype) // true
console.log(Person.prototype.__proto__ === Object.prototype) // true
console.log(Person.__proto__ === Function.prototype) // true
console.log(Function.prototype.__proto__ === Object.prototype) // true์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ฒซ๋ฒ์งธ ํธ์ถ์์ ์์ฑ์ํจ์์ ์ธ์คํด์ค๊ฐ ๊ฐ์ ํ๋กํ ํ์ ์ ๊ฐ๋ฆฌํค๋ ๊ฒ์ ์ ์ ์๊ณ , Person ์์ฑ์ ํจ์์ ํ๋กํ ํ์ ์ ํ๋กํ ํ์ ์ด Object.prototype์ด๋ผ๋ ๊ฒ์ ์ ์ ์๋ค. ๋ํ ๋นํธ์ธ ์์ฑ์ํจ์์ธ Function์ ํ๋กํ ํ์ ๋ Object.Prototype์ธ ๊ฒ์ ์ ์ ์๋ค.
โ ์ค๋ฒ๋ผ์ด๋ฉ๊ณผ ์๋์
ํ๋กํ ํ์ ์ฒด์ด๋์ผ๋ก ์์์ ๊ตฌํํ๋ฉด ์ธ์คํด์ค์ ์ ์ํ์ง ์์ ์์ฑ๋ค๋ ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค. ํ์ง๋ง ์ธ์คํด์ค์์ ๊ฐ์ ์ด๋ฆ์ ์์ฑ์ผ๋ก ๋ค๋ฅธ ๋ด์ฉ์ ๋ด๊ณ ์ถ์ ๋๊ฐ ์๋ค. ์ด๋ ์ผ์ด๋๋ ์ผ์ด ์ค๋ฒ๋ผ์ด๋ฉ๊ณผ ์๋์์ด๋ค.
function Person(name) {
this.name = name
}
Person.prototype.sayHello = function () {
console.log(`Prototype ${this.name}`)
}
const me = new Person("lee")
me.sayHello = function () {
console.log(`instance ${this.name}`)
}
me.sayHello() // instance lee
delete me.sayHello
me.sayHello() // Prototype leesayHello๋ฅผ ํ๋กํ ํ์
์ฒด์ธ์์ ์ฐพ์ ๋ ๊ฐ์ฅ ๋จผ์ ์ธ์คํด์ค์์ ์์ฑ์ ์ฐพ๊ธฐ ๋๋ฌธ์ ๋จผ์ instance lee๊ฐ ํธ์ถ๋ ๊ฒ์ ๋ณผ ์ ์๊ณ , ์ธ์คํด์ค์ sayHello๋ฅผ ์ ๊ฑฐํ ํ์๋ ์๊ธฐ ๋๋ฌธ์ ํ๋กํ ํ์
์ฒด์ธ์ ์ด์ฉํด Person.prototype์ sayHello๋ฅผ ์ด์ฉํด Prototype lee๊ฐ ํธ์ถ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ค์ฝํ์ฒด์ธ์์ ๊ฐ์ ์ด๋ฆ์ ์๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ฒ๋ผ ํ๋กํ ํ์ ์ฒด์ธ๋ ๋์ผํ๊ฒ ์๋ํ๋ค.
๐ ํ๋กํ ํ์ ๋ฐ๊พธ๊ธฐ
์์ prototype ๊ณผ __proto__๋ฅผ ์ด์ฉํด ์ ๊ทผํ ์ ์๋ค๋ ๊ฒ์ ์์๋ค. ์ด์ ๋ ์ง์ ์ ๊ทผํด์ ํ๋กํ ํ์
์ ๋ฐ๊ฟ๋ณด์
1) ์์ฑ์ํจ์๋ก ํ๋กํ ํ์ ๋ฐ๊พธ๊ธฐ
์์ฑ์ํจ์์์ ํ๋กํ ํ์
์ ์ ๊ทผํ๊ธฐ ์ํด์๋ prototype์ ์ด์ฉํ๋ฉด ๋๋ค.
function Person(name) {
this.name = name
}
Person.prototype = {
constructor: Person,
sayHello() {
console.log(this.name)
},
}
const me = new Person("lee")
console.log(me.constructor === Person)2) ์ธ์คํด์ค๋ก ํ๋กํ ํ์ ๋ฐ๊พธ๊ธฐ
์ธ์คํด์ค์์ ํ๋กํ ํ์
์ ์ ๊ทผํ๊ธฐ ์ํด์๋ __proto__๋ฅผ ์ด์ฉํ ์ ์์ง๋ง ์์ ์ ๋ฆฌํ ๊ฒ์ฒ๋ผ __proto__๋ก ์ ๊ทผํ ์ ์๋ ๊ฐ์ฒด๋ ์์ผ๋ฏ๋ก Object.getPrototypeOf์ Object.setPrototypeOf๋ฅผ ์ด์ฉํ ์ ์๋ค.
function Person(name) {
this.name = name
}
const me = new Person("lee")
const parent = {
sayHello() {
console.log(this.name)
},
}
Object.setPrototypeOf(me, parent)
me.sayHello()
console.log(me.constructor === Person) // false
console.log(me.constructor === Object) // true์ ์ฝ๋์์ me์ ํ๋กํ ํ์ ์ด ๋ฐ๋๋ฉด์ ๊ธฐ์กด์ Person.Prototype์ด constructor๊ฐ ์๋๋ผ parent์ constructor์ธ Object.Prototype์ด constructor๊ฐ ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ ๋ ๊ฐ์ง์ ์ฐจ์ด์ ์ ์์ฑ์ํจ์์ prototype์ด ์๋กญ๊ฒ ์ฐ๊ฒฐ๋ ํ๋กํ ํ์ ์ ๊ฐ๋ฆฌํค๊ณ ์๋๋ก ๋ณผ ์ ์๋ค. ์ด๋ ๊ฒ ์ง์ ํ๋กํ ํ์ ์ ๋ฐ๊ฟ์ ์์๊ด๊ณ๋ฅผ ๋ฐ๊ฟ ์ ์์ง๋ง, ์ํํ๋ค๊ณ ์๊ฐ๋๋ค. ์์์ด ๋ถ๋ชจ๋ฅผ ๋ฐ๊พธ๋ค ๋ณด๋ฉด ๊ด๊ณ๊ฐ ๊ผฌ์ผ ์๋ ์๊ธฐ ๋๋ฌธ์ ๋์ ์ผ๋ก ๋ฐ๊พธ์ง ์๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ๋ค.
๊ทธ๋ฌ๋ฉด ํ๋กํ ํ์ ์ ๋ฐ๊พธ์ง ์๊ณ , ์์๋ฐ์ ์์ฑ์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ด๋ค ํ๋กํ ํ์ ์ด ํ๋กํ ํ์ ์ฒด์ธ์ ์๋์ง ์๊ณ ์ถ์ ์ ์๋ค. ํ๋กํ ํ์ ์ ํด๋น ๊ฐ์ฒด๊ฐ ์๋ ์ง ์ด๋ป๊ฒ ํ์ธํ ์ ์์๊น?
๐ Instanceof
Instanceof ํค์๋๋ ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ฒด์ธ์ ์์ฑ์ํจ์์ ํ๋กํ ํ์ ์ด ์กด์ฌํ๋์ง ํ์ธํ ์ ์๋ค.
function Person(name) {
this.name = name
}
const me = new Person("Choi")
console.log(me instanceof Person) // true
console.log(me instanceof Object) // true
const parent = {}
Object.setPrototypeOf(me, parent)
console.log(me instanceof Person) // false
console.log(me instanceof Object) // true์ ์ฝ๋์์ Object.setPrototypeOf์ ์ด์ฉํด me์ prototype์ด ๋ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ๋ฐ๋๊ณ ๋ ํ์ instanceOf๋ฅผ ์ด์ฉํด ํ์ธํ์ ๋, ๊ธฐ์กด ํ๋กํ ํ์
์ด์๋ Person.Prototype์ด ํ๋กํ ํ์
์ฒด์ธ์์ ์ฌ๋ผ์ง๊ฒ ๋์ด false๋ก, ์๋กญ๊ฒ ๋ถ๋ชจ๊ฐ ๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ Prototype์ธ Object.Prototype์ true ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
function Person(name) {
this.name = name
}
const parent = { x: 1 }
Person.prototype = parent
const me = new Person("hi")
console.log(parent.constructor === Person) //false
console.log(me) // { name: 'hi' }
console.log(me instanceof Person) //true์ด ๋ถ๋ถ์ ์ ๋ฆฌํ๋ฉด์ ํท๊ฐ๋ ธ๋ ๊ฒ์ ์์ฑ์ ํจ์์ ํ๋กํ ํ์
์ parent๋ก ๋ฐ๊พผ๋ค๊ณ ํด์ ์์ฑ์ ํจ์๋ฅผ ์ด์ฉํด ๋ง๋ค์ด์ง ์ธ์คํด์ค์ ๋ชจ์ต์ ์ํฅ์ ์ฃผ์ง ์๋๋ค๋ ๊ฒ์ด์๋ค. ์ธ์คํด์ค์ ํ๋กํ ํ์
๋ง ๋ฐ๋ ๋ฟ, me์์ฒด๊ฐ { x: 1 }์ด ๋๋ ๊ฒ์ด ์๋๋ค. ๊ทธ๋ฆฌ๊ณ ์๋กญ๊ฒ ๋ฐ๊พผ ํ๋กํ ํ์
์ constructor๊ฐ ์ธ์คํด์ค์ ์์ฑ์ํจ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์ง ์์๋ ํ๋กํ ํ์
์ฒด์ธ์ ์กด์ฌํ๋ค๋ฉด instanceOf๋ true๋ฅผ ๋ฐํํ๋ค.
๐ Object.create
๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ ์ค ๋ง์ง๋ง ๋ฐฉ๋ฒ์ด์๋ Object.create()๋ ํ๋กํ ํ์
์ ์ง์ ์ ํด์ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. ์ง์ ์์์ ์ ํด ์ฃผ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ new ํค์๋ ์์ด๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๊ณ , ๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ ์์๋ฐ์ ์ ์๋ค.
const obj1 = Object.create(null)
console.log(obj.toString()) // TypeError: obj.toString is not a function
const parent = { x: 1 }
const child = Object.create(parent)
console.log(child.x) // 1
console.log(Object.getPrototypeOf(child) === parent) // true์ ์ฝ๋์์ null์ ํ๋กํ ํ์ ์ผ๋ก ๋ง๋ ๊ฐ์ฒด๋ ํ๋กํ ํ์ ์ ์ข ์ ์ ํด๋นํ๊ธฐ ๋๋ฌธ์ Object.Prototype์ ๋ด๋ถ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค. Object.create์ Parent๋ฅผ ์ด์ฉํด child๋ฅผ ๋ง๋ค๋ฉด, child๋ parent์ x์์ฑ์ ์์ ๋ฐ๊ณ parent๋ฅผ ํ๋กํ ํ์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
๐ฎ ์ ์ ์์ฑ/๋ฉ์๋
์ ์ ์์ฑ/๋ฉ์๋๋ ์์ฑ์ ํจ์ ์์ฒด๊ฐ ์์ ํ๋ ์์ฑ๊ณผ ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์ ์ธ์คํด์ค์์ ์ฐธ์กฐํ ์ ์๋ ํน์ง์ ๊ฐ์ง๋ค.
function Person(name) {
this.name = name
}
Person.prototype.sayHello = function () {
console.log(this.name)
}
Person.staticProp = `static Prop`
Person.staticMethod = function () {
console.log("static method")
}
const me = new Person("choi")
Person.staticMethod() // static method
me.staticMethod() // TypeError: me.staticMethod is not a function์ ์ฝ๋์์ ์์ฑ์ ํจ์ ์์ฒด๊ฐ ๊ฐ์ง๋ ์์ฑ๊ณผ ๋ฉ์๋๋ ์ธ์คํด์ค์์ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์์ ๋ณธ ๊ทธ๋ฆผ์์ ์์์ ํ๋กํ ํ์ ์ฒด์ธ์ ์ด์ฉํด ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์ ์์ฑ์ ํจ์ ์์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ static ์์ฑ๊ณผ ๋ฉ์๋๋ ์์๋ฐ์ ์ ์๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
๐ค ๊ฐ์ฒด ์ ์์ฑ ์กด์ฌ ์ฌ๋ถ ํ์ธ๊ณผ ๋์ด
1) ์์ฑ ์กด์ฌ ์ฌ๋ถ
๊ฐ์ฒด์ ์์ฑ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์ผ๋ก in์ ์ฌ์ฉํ ์ ์๋ค. ์ด๋ ํ๋กํ ํ์
์ผ๋ก ์์๋๋ ์์ฑ๋ค๋ ๋ค ํ์ธํ ์ ์๋ ํน์ง์ ๊ฐ์ง๋ค.
const Person = {
name: "choi",
}
console.log("name" in Person) // true
console.log("hasOwnProperty" in Person) // true๋ง์ฝ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ณ ์ ์์ฑ๋ง ํ์ธํ๊ณ ์ถ๋ค๋ฉด hasOwnProperty๋ฅผ ์ด์ฉํด์ ํ์ธํ ์ ์๋ค.
const Person = {
name: "choi",
}
console.log(Person.hasOwnProperty("name")) // true
console.log(Person.hasOwnProperty("hasOwnProperty")) //false2) ๋์ดํ๊ธฐ
๊ฐ์ฒด ํ๋กํผํฐ๋ฅผ ์ํํ๋ฉฐ ๋์ดํ๋ ๋ฐฉ๋ฒ์ผ๋ก for~in ๊ตฌ๋ฌธ์ด ์๋ค. ์ด๋ ์ฃผ์ํ ์ ์ ๊ณ ์ ์์ฑ ๋ฟ ์๋๋ผ ์์ ๋ฐ์ ์์ฑ ์ค [[Enumerable]]์ด true์ธ ๊ฐ๋ค๋ ์ด๊ฑฐํ๋ค.
const Person = {
name: "choi",
}
Object.setPrototypeOf(Person, { x: 1 })
for (const key in Person) {
console.log(key)
}
// name
// x๊ฐ์ฒด๊ฐ ๊ฐ์ง ์์ฑ๋ค๋ง ๋์ดํ๋ ค ํ๋ค๋ฉด Object.keys(),Object.values(),Object.entries()์ ๊ฐ์ ๋ฉ์๋๋ฅผ ์ด์ฉํ ์ ์๋ค.
const Person = {
name: "choi",
}
Object.setPrototypeOf(Person, { x: 1 })
console.log(Object.keys(Person)) // ["name"]
console.log(Object.values(Person)) // ["choi"][์ฐธ๊ณ ]