νΉμ κ°μ²΄λ₯Ό μμ μ΄ λΆκ°λ₯νκ² λ§λ€ λ, λΆλ³ κ°μ²΄λ₯Ό λ§λ€μ΄ μ¬μ©ν©λλ€.
λΆλ³ κ°μ²΄λ₯Ό λ§λλ λ°©λ²μ 3κ°μ§ μ λλ€.
κ° λ°©λ²λ§λ€ μμ κ°λ₯ λ²μκ° λ€λ₯΄κ² μ νλκΈ° λλ¬Έμ νμμ λ°λΌ μ ννλ©΄ λ©λλ€.
1. Object.freeze() μμ± μ¬μ μ X, μΆκ° X, μμ X, μμ X
κ·Έλ¬λ! μ€μ²©λ company λ freeze λμ§ μμ΅λλ€. λλ¬Έμ company.name μ μμ κ°λ₯ν©λλ€.
μμ± μ¬μ μκ° μλλ€λ κ²μ Object.defineProperty() λ‘ μμ±λ€μ μ¬μ μ ν μ μλ€λ λ»μ λλ€. μλ λ§ν¬ μ°Έκ³ .
const company = { name: 'SS'};
const people = { name: 'lee', age: 20, work: company }
Object.freeze(people);
people.name = 'park'; // μΆκ°
people.color = 'red'; // μμ
console.log(people); // { name: 'lee', age: 20, work: { name: 'SS' } }
κ°μ²΄κ° λ³νμ§ μμ κ²μ μ μ μμ΅λλ€.
2. Object.seal() μμ± μ¬μ μ X, μΆκ° X, μμ O, μμ X
3. Object.preventExtensions() μμ± μ¬μ μX, μΆκ° X, μμ O, μμ O
π Object λκ²° μ¬λΆ νμΈ
console.log(Object.isFrozen(company)); // true or false λ°ν
console.log(Object.isSealed(company));
console.log(Object.isExtensible(company));
μ°Έκ³ μλ£
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Object.defineProperty() - JavaScript | MDN
Object.defineProperty() μ μ λ©μλλ κ°μ²΄μ μλ‘μ΄ μμ±μ μ§μ μ μνκ±°λ μ΄λ―Έ μ‘΄μ¬νλ μμ±μ μμ ν ν, ν΄λΉ κ°μ²΄λ₯Ό λ°νν©λλ€.
developer.mozilla.org
'JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JS ν¨μ μ μΈλ¬Έκ³Ό ν¨μ ννμ μ°¨μ΄ (0) | 2022.08.11 |
---|---|
JS μμ 볡μ¬μ κΉμ λ³΅μ¬ (0) | 2022.08.11 |
JS Truthy μ Falsy (0) | 2022.08.10 |
JS ν λ³ν (λ¬Έμλ₯Ό μ«μλ‘, μ«μλ₯Ό λ¬Έμλ‘), λ³μμ νμ νμΈ typeof (0) | 2022.08.10 |
JS κΈ°λ³Έν λ°μ΄ν°μ μ°Έμ‘°ν λ°μ΄ν°μ μ°¨μ΄ (0) | 2022.08.10 |