Sitemap
Tommaso Rossi

Tommaso Rossi

Bitforce.app CTO, Front end Lover

Constant

1 min readJun 24, 2016

--

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

Get Tommaso Rossi’s stories in your inbox

Join Medium for free to get updates from this writer.

//Versione ES5
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.

Press enter or click to view image in full size
Constant in ES6

--

--