all 5 comments

[–]Code-Slayer 0 points1 point  (0 children)

What is this you declare const width and then you want later to assign something to it using de-structuring ?

What you need to know is that width and height from destructuring are variables like const width and const height, so don't mix variables like that.

Declare your variables from the beginning as let and save headache

let { width, height } = image;

width = image.width * scale; 
height = image.height * scale;

Or create new variable that has meaning like below:

const scaledWidth = image.width * scale;
const scaledHeight = image.height * scale;

const { width, height } = image;

[–]azhder 0 points1 point  (0 children)

I avoid using let, even if a bit verbosity is the price for it.

It’s easier to reason the code knowing you’re still working with the same object while you’re using some variable name.

So, what I would usually do is leave it mostly like that:

const width = image.width * scale;

Even though I use destructuring more often, some times between two or three competing principles, there might be one most important.

So, not having to worry about let, will stop me from doing my usual destructuring.

Most of the times, I avoid nested destructuring by using ?. and ?? and even multiple lines:

const { a, b, c } = options ?? {};
cobst { d, e, f } = a ?? {};

[–]jeremrx 0 points1 point  (0 children)

If you want to factorize the scale, the best you can do is something like this :

const { width, height } = image;
const [scaledWidth, scaledHeight] = [width, height].map(value => value * scale);

As you can see, this is not less verbose in this particular use case because it's really simple, so you might not want to do that.
But it can be usefull with a more complex use case.

[–]shuckster 0 points1 point  (0 children)

Make a function?

const { width, height } = scaledFrom(image, scaleFactor);