What I want to do is to use as many immutable variables as possible, thus reducing the number of moving parts in my code. I want to use "var" and "let" only when it's necessary.
This won't work:
function constParam(const a){ alert('You want me to '+a+'!'); }
Any ideas?
To create a constant, As first parameter, we are passing either window object or global object. As second parameter, we are passing name of the variable to be created, which is foo in this case. As third parameter, we are passing object to configure the variable behavior.
Within function definition bodies, the parameters may be used like any other variable. But the parameters are constant in the sense that they can't be assigned to (i.e., can't appear on the left side of an assignment ( = ) statement)<. In other words, their value remains constant throughout the function body.
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
For some reason, the parameters in the object are already treated as members without assigning them to any member variables created in the constructor of the object. The parameters are mutable also as seen in the code block below.
Function parameters will stay mutable bindings (like var
) in ES6, there's nothing you can do against that. Probably the best solution you get is to destructure the arguments
object in a const
initialisation:
function hasConstantParameters(const a, const b, const c, …) { // not possible … }
function hasConstantParameters() { const [a, b, c, …] = arguments; … }
Notice that this function will have a different arity (.length
), if you need that you'll have to declare some placeholder parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With