48
loading...
This website collects cookies to deliver better user experience
const
keyword can't be changed. Although this statement is true in the case of primitive values, it is complicated when it comes to objects and arrays.const foodsILike = ['Shwarma']
foodsILike.push('Jalebi')
console.log(foodsILike) // => ["Shwarma", "Jalebi"]
const
keyword and were still able to add items to itObject.freeze()
method freezes
an object (duh!). What does this exactly mean? A frozen object cannot be edited. New properties cannot be added and existing properties cannot be removed or modified in any way.'use strict'
const obj = {
prop: 42,
}
Object.freeze(obj)
obj.prop = 33
// => TypeError: "prop" is read-only
// => will fail silently in non strict mode
'use strict'
const obj = {
prop: 42,
propObject: {
name: null,
},
}
Object.freeze(obj)
obj['propObject']['name'] = 'Burhan'
console.log(obj)
/**
{
prop: 42,
propObject: {
name: "Burhan"
}
}
*/
var deepFreeze = require('deep-freeze')
deepFreeze(Buffer)
Buffer.x = 5
console.log(Buffer.x === undefined)
Buffer.prototype.z = 3
console.log(Buffer.prototype.z === undefined)
/**
true
true
*/
const
keyword creates a read-only reference to a value. It does not mean that the value itself is immutable. It's just that the variable identifier cannot be reassigned