In Javascript, how would you make a variable read-only?
Réponses aux questions d'entretien
Utilisateur anonyme
24 sept. 2015
declaring a variable using const keyword. This declares a read-only named constant
5
Utilisateur anonyme
15 sept. 2015
There are few solutions, one is to use revealing modular pattern where you return function that only retrieves value of property, and everything is wrapped in closure. Other solution is to use Object.defineProperty where you cane be specific and set write value of object property.
2
Utilisateur anonyme
2 sept. 2017
There is no "const" keyword back in 2013 when the question was asked.
Utilisateur anonyme
4 déc. 2017
var obj = {};
Object.freeze(obj);
// Or when defining a property
Object.defineProperty(obj, 'key', {
writable: false,
value: 'read-only variable'
});
1
Utilisateur anonyme
16 oct. 2019
This looks so good:
bit.ly/faang100
Utilisateur anonyme
27 févr. 2014
create the variable using the var keyword, then create a function that returns its value