“cara membuat objek abadi di javascript” Kode Jawaban

cara membuat objek abadi di javascript

The Object.freeze() method freezes an object: 
that is, prevents new properties from being added to it; 
prevents existing properties from being removed; 
and prevents existing properties, or their enumerability, 
configurability, or writability, from being changed, 
it also prevents the prototype from being changed. 
The method returns the object in a frozen state.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

const obj = {
  prop: 42
};

Object.freeze(obj);

obj.prop = 33;
// Throws an error in strict mode

console.log(obj.prop);
// expected output: 42
Friendly Fowl

Buat Objek Immutable di JavaScript

let x = {name: "blood", color: "red"}
let {...y} = x

// let's update the value of "x"
x.name = "strawberry"

console.log(x) // will return {name: "strawberry", color: "red"}
console.log(y) // will return {name: "blood", color: "red"}
/* "y" haven't changed because it copied the value of "x" and made himself
a whole separated ohject, instead of just coping the reference of "x"
(reference copy case:- let y = x) */
/* NOTE: get some youtube classes about javascript reference type and primitive
data types, if you're not clear enough about what i mean by "reference" */
maldito codificador

Jawaban yang mirip dengan “cara membuat objek abadi di javascript”

Pertanyaan yang mirip dengan “cara membuat objek abadi di javascript”

Lebih banyak jawaban terkait untuk “cara membuat objek abadi di javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya