This question already has answers here:
es6 how to use default parameters that go before non-default parameters?
(4 answers)
Closed 2 years ago.
It's practical to have the last parameter with a default value, as follows.
function add(x, y = 5) {
return x + y;
}
console.log(add(3)); // results in '8'
However, is it possible to have other than the last parameter with a default value? If so, how would you call it with the intention of using the first default value, but providing the second parameter?
function add(x = 5, y) {
return x + y;
}
console.log(add(,3)); // doesn't work
You still need to provide the first parameter regardless of its default value.
Try destructuring a single parameter instead, like this:
function add({ x = 5, y }) {
return x + y;
}
console.log(add({ y: 3 })); // results in '8'
You will still have to specify the y key, though, but this is a way better practice. 😄
You still could keep your add function the same by call it with params from destructed array like below snippet
function add(x = 5, y) {
return x + y;
}
console.log(add(...[,3]));
Related
This question already has answers here:
Set a default parameter value for a JavaScript function
(29 answers)
Closed 11 months ago.
In JavaScript it's possible to define a function like this:
function func(a = 10, b)
{
return a + b;
}
How does one call this function by only specifying the value for b?
It'd only be possible by explicitly passing undefined, and by also providing a default for the second argument.
function func(a = 10, b) {
return a + b;
}
console.log(func(undefined, 5));
But this is quite weird - it's very confusing. I'd highly recommend not doing this at all - either put the arguments with default values at the end, or in an object.
function func({ a = 10, b }) {
return a + b;
}
console.log(func({ b: 5 }));
If you are okay with currying the first parameter:
bind saves the function with the arguments supplied, so when one calls it, it calls it with the arguments applied. The first argument is what to bind this to, so the second argument will bind to a:
function func(a, b) {
return a + b;
}
newFunc = func.bind(null, 10)
newFunc(2)
12
This question already has answers here:
ES6 destructuring function parameter - naming root object
(5 answers)
Closed 11 months ago.
Is it possible to do something like the following in js?
function Something( Point{PointX, PointY} ) {
console.log('Point', Point);
console.log('PointX,PointY', PointX, PointY);
}
Something([1,2]);
If so, what would be the proper way to do that?
function Something(Point) {
const [PointX, PointY] = Point
console.log('Point', Point)
console.log('PointX,PointY', PointX, PointY)
}
Is this what you need?
function Something([PointX, PointY]) {
console.log('PointX,PointY', PointX, PointY);
}
You can destructure both arrays (function([x, y]) and objects (function({ x, y })).
Sure, but make sure the keys match up to do the destructuring assignment for an object, such as:
function Something({x,y}) {
console.log('PointX, PointY', x, y);
}
Something({x: 1, y: 2});
In terms on naming and also assigning it to the outer object, perhaps you can do something like this instead:
function Something(Point) {
const {x,y} = Point;
console.log('Point, PointX, PointY', Point, x, y);
}
Something({x: 1, y: 2});
I was wondering if there's a way of changing some variable value with the return of some function. A short-hand-way of doing it.
If I want to add some value to a variable and changing it, we do like that:
let numb = 5;
numb *= 2; // Returns 10
But lets say I have a function that return the double of its argument like this:
function double(a) {
return a * 2;
}
let numb = 5;
numb = double(numb); // Short hand of doing this line <----
You need to understand how reference and value parameters work. When you pass in a number or a string to a function that is a value parameter and changing the value of the parameter withing the function has no effect on the variable that was passed in. If the parameter is a reference to an object then changes to properties on that object will then change the variable passed in.
let x = 5;
someFunc(x);
There is no way for someFunc to change x because the value 5 of x was passed into the function, not a reference to x;
let x = { prop: 5 };
someFunc(x);
Now if the body of someFunc changes x.prop then it will also change it to the variable x that was passed in because a reference to an object instance was passed in.
It is the same as assigning variables.
let x = 5;
let y = x;
Now x and y are both 5 but changing y = 6 does not effect x.
let x = { prop: 5 };
let y = x;
Now y is a reference to the same object so y.prop = 6 will change x as well.
All that aside good programming principles and modern functional programming concepts dictate that modifying parameters passed into functions is not good practice.
You could use an arrow key function. Saves a line or two.
const double = a => a * 2;
let numb = 5;
numb = double(numb);
Removing the short hand, the double function would look like
const double = (a) => {
return a * 2
}
Arrow key functions are pretty much the same as a normal function, however they give you more control over the 'this' keyword.
This question already has answers here:
Callback function - use of parentheses
(4 answers)
Closed 6 years ago.
Why don't I need to use () to call a function within a 'for' loop or within an '.addEventListener()' method? For example:
function activitiesReset()
{activitiesLegend.style = '';
}
for (var i=0; i<checkboxes.length; i++) {
checkboxes[i].addEventListener('change', activitiesReset);
}
In the addEventListener method, I first tried calling my function like activitiesReset(), but this did not work. Removing the () from the end of the function worked.
Why is that?
checkboxes[i].addEventListener('change', activitiesReset) is not calling activitiesReset. It is passing activitiesReset as an argument. activitiesReset is a function itself; activitiesReset() is a call to a function, evaluating to its return value. You want to set the event listener to your function, not to undefined (which is the result of calling your function).
The key to understanding this is the fact that functions in JavaScript are also values, just like numbers or strings or objects. (In fact, they are a specific kind of object.)
var adderFuncValue = function(x, y) { return x + y; };
function multiplierFuncValue(x, y) { return x * y; };
function callFuncWithTwoParams(func, p1, p2) {
func(p1, p2);
}
callFuncWithTwoParams(adderFuncValue, 3, 5); // 8
callFuncWithTwoParams(multiplierFuncValue, 2, 3); // 6
var someFunc = adderFuncValue;
someFunc(7, 8); // 15
So just like I'm passing functional values into callFuncWithTwoParams, you are passing a functional value into addEventListener. The browser will remember that function and associate it with change event, and call it -- later.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Pass Variables Through Reference
How can you do something like this in javascript?
In PHP you can put a & in a function in front of the parameter to write directly back to the variable.. How can you do something like this in javascript?
In JavaScript, values are passed to functions by value. Objects, however, are passed by reference.
Passing values:
function myfunction(x)
{
// x is equal to 4
x = 5;
// x is now equal to 5
}
var x = 4;
alert(x); // x is equal to 4
myfunction(x);
alert(x); // x is still equal to 4
Passing objects:
function myobject()
{
this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6
Source: http://snook.ca/archives/javascript/javascript_pass
If you pass an object into a function it is always treated as passing a reference to it.
To have a similar effect for primitive values, you might wrap them in an object before like
var intVal = { val: 0 };
That way you can pass it to a function and still modify the actual variable.