Possible to assign function parameter via destructuring? [duplicate] - javascript

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});

Related

Destructuring within an argument list

Let's say I have the following function:
function get_scaled_coordinates(x, y, scale) {
return [x*scale, y*scale];
}
Is it possible if I have a single variable with [x,y] to pass to the function. Perhaps something similar to this, conceptually speaking:
let point = [1,1];
get_scaled_coordinates(**point, scale);
// instead of having to do get_scaled_coordinates(point[0], point[1], scale)
You could take an array as target for destructuring.
function get_scaled_coordinates([x, y], scale) {
return [x * scale, y * scale];
}
If you like to use higher dimensions, you could map the array with new values.
function get_scaled_coordinates(coordinates, scale) {
return coordinates.map(v => v * scale);
}
Perhaps something similar to this, conceptually speaking:
get_scaled_coordinates(**point, scale);
You seem to be looking for spread syntax in the function call:
get_scaled_coordinates(...point, scale);
No need to change the function, making it accept an array.

Is having the first JavaScript parameter with default value possible? [duplicate]

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]));

JavaScript array expressions return object literal in one line [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
Its known that someone can make a one-line array function like this to return the single value:
var func = (x) => x + 1 //func(2) == 3
and you can also make multi-line array-functions, whose values need to be manually returned:
var funcMultiline = (x) => {
var result = 1;
result += x;
return result;
}
funcMultiline(4) == 5; //true
So the question:
let's say I want to return a new object in one line, if I use the brackets, then the array-function is treated like a multi-line function, and doesn't actually return the object-literal. Is there any direct way to create an object literal in, lets say, a map function? Like:
[...Array(25)].map(e => {x: 5, y:10}) //this is a syntax error, but how can I make this work
Returning object literals using the concise body syntax params => {object:literal} will not work as expected.
You have to wrap the object literal with parenthesis:
var res = [...Array(25)].map(e => ({x: 5, y:10}))
console.log(res);

Function.prototype.call doesn't work inside higher order functions and/or arrow functions? [duplicate]

This question already has answers here:
Arrow Functions and This [duplicate]
(5 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 5 years ago.
I try to use higher order functions (map, find, filter, reduce, etc) on an array of objects. In the callback I need to use a function inside of those objects, with the correct 'this' argument. Something like this:
const something = [
{
x: 42,
f: y => this.x + y
},
{
x: 13,
f: y => this.x - y
}
]
const result = something.map(obj => obj.f.call(obj, 10));
I expected result to be [52, 3], but I've got this:
TypeError: Cannot read property 'x' of undefined
Could you help me
understand what happened here
forward 'this' inside my function?
Thanks!

Can you change the same property for more than one variable with one line of code in Javascript?

If I have many variables with the same property x, for example,
a.x
b.x
c.x
Can I change the value of x for all these variables at one time? Something like:
*.x = 200; // Change the value of "x" in every variable that has a property "x" to 200.
Is it possible? And if so, how?
No. You can't set multiple variables of the same type at once using a wildcard like you propose.
You can concatenate statements though like this:
left.x = right.x = 200;
Or use a list or dictionary like object in javascript and iterate over every key that matches.
If you gather your variables inside a common object like so:
var points = { a: { x: 1, y: 2}, b: { x: 3, y: 4 }, c: {x: 5, y, 6 }
then it can almost be done (minus the neat wildcard notation). Here's a function that should fit your need:
function setPropertyForAll(container, propName, newValue) {
for (var key in container) {
if (container.hasOwnProperty(key) && container[key] !== null && container[key].hasOwnProperty(propName)) {
container[key][propName] = newValue;
}
}
}
Usage:
setPropertyForAll(points, "x", 42);

Categories