http://stackoverflow.com/questions/30314457/how-to-make-function-parameter-constant-in-javascript
Const will only prevent re-assigning
It will not prevent modification of the attributes of the object.
For example, this will give you an error because you are trying to reassign the variable car to another object.
1 2 3 4 5 6 7 8 |
const car = { type: "bmw", color: "white" }; car = { }; |
But if you were to change the attribute of the object, it is valid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const car = { type: "bmw", color: "white" }; function changeCar(carObj) { if('type' in carObj) { carObj.type = "honda"; } } changeCar(car); |
You’d see honda, white
http://stackoverflow.com/questions/30314457/how-to-make-function-parameter-constant-in-javascript
Another example:
However, bear in mind that const in ES6 notation does not make the variable immutable.
1 |
const a = [1, 2]; a.push(3); |
is a completely valid program and a will become [1, 2, 3]. const will only prevent you from reassigning a, so that you can’t do a = [] or a = {} or whatever once const a = [1, 2]; already defined (in that particular scope).
Read Only Variable
ref – http://stackoverflow.com/questions/2821509/can-you-use-constant-variables-in-javascript
if you’re looking for a read-only variable, you simulate that with something like
1 2 3 4 |
var constants = new (function() { var x = 20; this.getX = function() { return x; }; })(); |
and then use it like
1 |
constants.getX() |