Constant
Questo è un articolo che fa parte di una guida su ES6
Benvenute costanti
ES6 ha introdotto la possibilità di dichiarare costanti, nel modo in cui tutti ci aspettavamo.
const REAL_CONST = ‘const’;L’ alternativa che abbiamo con ES5 è la seguente
//Versione ES5Get Tommaso Rossi’s stories in your inbox
Join Medium for free to get updates from this writer.
var obj = {
val: ‘test val’
};Object.defineProperty(obj, “CONST_VAL”, {
value: ‘fixed val’,
enumerable: true,
writable: false,
configurable: false
});console.log(obj);
obj.val = ‘edited val’;
obj.CONST_VAL = ‘i can not change’;
console.log(obj);
Proviamola subito
In questo jsbin potete verificare il suo funzionamento e l’errore che viene notificato in console se tentiamo di modificarne il suo valore.








