Basit olalım.
Onlar farklı. Her durumu açıklayacak olan koddaki yorumları kontrol edin.
Const- letHangi değerin yeniden atanamayacağı, yeniden bildirilmiş gibi blok kapsam değişkenidir .
Bunun anlamı
{
const val = 10; // you can not access it outside this block, block scope variable
}
console.log(val); // undefined because it is block scope
const constvalue = 1;
constvalue = 2; // will give error as we are re-assigning the value;
const obj = { a:1 , b:2};
obj.a = 3;
obj.c = 4;
console.log(obj); // obj = {a:3,b:2,c:4} we are not assigning the value of identifier we can
// change the object properties, const applied only on value, not with properties
obj = {x:1}; // error you are re-assigning the value of constant obj
obj.a = 2 ; // you can add, delete element of object
Bütün anlayış, const'ın blok kapsamı olduğu ve değerinin yeniden atanmadığıdır.
Object.freeze:
Nesne kök özellikleri değiştirilemez, ayrıca daha fazla özellik ekleyip silemeyiz ancak tüm nesneyi yeniden atayabiliriz.
var x = Object.freeze({data:1,
name:{
firstname:"hero", lastname:"nolast"
}
});
x.data = 12; // the object properties can not be change but in const you can do
x.firstname ="adasd"; // you can not add new properties to object but in const you can do
x.name.firstname = "dashdjkha"; // The nested value are changeable
//The thing you can do in Object.freeze but not in const
x = { a: 1}; // you can reassign the object when it is Object.freeze but const its not allowed
// Her ikisinde de benzer olan bir şey, yuvalanmış nesnelerin değiştirilebilir olmasıdır
const obj1 = {nested :{a:10}};
var obj2 = Object.freeze({nested :{a:10}});
obj1.nested.a = 20; // both statement works
obj2.nested.a = 20;
Teşekkürler.