This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Closed 6 years ago.
Why does node.js allow a module (or an object) specified as a constant to be changed?
For example, this is allowed:
const EXPRESS = require('express');
EXPRESS.someProperty = 'some value';
But this is not:
const MYCONST = '123';
MYCONST = '456';
const means that you cannot change the reference itself, not what the reference points to.
const a = { name: 'tom' };
// you cannot change the reference (point a to something else)
a = 5; // this is an error
// but you can change the value stored at that reference with no problem
a.name = 'bob';
const does not make the object to become immutable thus you can change the object itself but you can not assign another object to that reference
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const
From the docs:
The const declaration creates a read-only reference to a value. It
does not mean the value it holds is immutable, just that the variable
identifier cannot be reassigned. For instance, in case the content is
an object, this means the object itself can still be altered.
This isn't node-specific, it is part of the Javascript spec. The reference EXPRESS is the constant, and when you declare using const you are not allowed to reassign the reference.
const EXPRESS = require('express');
EXPRESS = 'something else';
would also fail
Related
This question already has answers here:
How do I make a JavaScript variable completely immutable?
(3 answers)
Closed last year.
In javascript, if I create a const array, I can still modify the object the variable points to:
// The const declaration creates a read-only reference to a value.
// It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
const x = [1,2,3];
x.push(4);
console.log(x);
x=55 // But this is illegal and will error
console.log(x);
Is there a way to make the elements in an array immutable as well? Similar to something like const int* const x; in C?
You can use Object.freeze to prevent an object (or array) from being changed:
const x = [1, 2, 3];
Object.freeze(x);
x.push(4); // This will throw an exception
objects frozen with Object.freeze() are made immutable.
Here are the docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
Example:
const x = [1,2,3];
Object.freeze(x);
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
Is possible in javascript define an object field name based on the value of a variable INLINE?
for exemple:
const field = "name";
const js = {field:"rafael"};
console.log(js);
the result of this code will be {field:rafael} but the result I want is {name:rafael}.
I know I can do
const field = "name";
const js = {};
js[field] = "rafael";
but i would like to do inline as I initialize the object. Is that possible?
The es6 version of JavaScript allows you to handle this issue, we can use variables while creating the object to dynamically set the property like so:
const field = "name";
const js = {[field] : "rafael"};
console.log(js);
Setting dynamic property keys - https://www.c-sharpcorner.com/blogs/how-to-set-dynamic-javascript-object-property-keys-with-es6
This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
Apologies if this is simple. I just couldn't figure-out how to do it. Searched on this site on how to dynamically change variables and evaluate them etc but can't figure out how to do what I want.
Problem: I have a variable that I set to a value:
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
//Here
};
Where it says "Here", I need to instantiate a new variable that is called whatever is the result if the above statement eg; switch1 then set it true or false as a boolean. eg: switch1 = false;
Therefore again, if $switchToggle parameter was "Test", I need to dynamically create a variable called switchTest and set it true or false.
Is such a thing possible ?
Thanks all
Something like that?(I created vm variable just for code snippet, ignore it)
var vm = {};
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
vm[vm.switchValue] = 'whatever';
console.log(vm);
};
vm.toggleDrop('true');
Also if you do not need to attach variable to vm object, best answer will be #nastyklad provided(using window[vm.switchValue]
Something like this:
function setVariable(name){
var variableName = 'switch' + name;
vm[variableName] = true;
}
This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
JavaScript const Keyword
(8 answers)
Closed 7 years ago.
Using JavaScript ES6, I am surprised that doing:
const a = {};
a.foo = 'bar';
a.foo = 'car';
Is valid. Why is this? I would have assumed const would mean you cannot change the a empty object and apply new properties. Even further, I would also have assumed you cannot change the value of a property of a once it is set.
Only the variable assignment is constant. Any objects or arrays referenced stay mutable.
const a = {one: 1};
a.three = 3; // this is ok.
a = {two: 2}; // this doesn't work.
What you can do is use Object.freeze:
const a = {one: 1};
Object.freeze(a);
a.three = 3; // silently fails.
// a is still {one: 1} here.
No, const a means you cannot change the value of the variable a. Its value is always the same object; changing properties of an object doesn't make it into a different object.
Using an analogy, I am the same Person whether amadan.jacket = null or amadan.jacket = "Heavy Winter Jacket". amadan is constant.
To make the properties immutable, you would either have to make the properties explicitly readonly by writable: false, or use Object.freeze or Object.seal (differences) to make the entire object immutable.
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I wanted to create variable name based on value sent to function in javascript.
like following, when i call function variable : variable("zebra"); this should return variable name as zebra1
function create variable(i){
return i+"1";
}
var variable("zebra")="abc";//this line should create variable zebra1 and initialise as abc
Try:
window['zebra'] = 'abc';
The window object holds all the global variables, assuming this is a request for global variables.
To specifically answer your question, you could put return window[i + '1'] = 'abc'; in your variable naming function.
Alternatively, you could create a global (or local) object named variables to hold all your variables:
function whoknows() {
var variables = {};
variables['zebra'] = 'abc';
}
Read more on working with objects at mozilla.org
You can create global variable with
window["zebra"] = "abc";
and use later ether with the same indexer syntax or directly - zebra.