I am a software developer who is now learning the new ES6 style of JavaScript. I find the ES6 features great, but I'm still not aware of all the most popular features that ES6 offers and I find it difficult to use it wisely in practice.
I have the following line of code that - if possible and practical - I'd like to make simpler. I have to admit that in this example, I would also be refactoring just for the sake of refactoring and learning better how ES6 can help me creating expressive, simple code in the future (or at least understand if someone uses some crazy one-liner).
// HTML: <canvas></canvas>
const canvas = document.querySelector('canvas');
// I'm wondering if there is another way to do this:
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
I could create width and height like this:
const { innerWidth: width, innerHeight: height } = window;
// equivalent to:
const width = window.innerWidth;
const height = window.innerHeight;
I am looking for a solution that rather than creating new variables, assigns the values to an object, using some property names. Is there an idiomatic way to do this?
Something like this:
const canvas = document.querySelector('canvas');
// NONE OF THESE WORKED
const { innerWidth: canvas.width, innerHeight: canvas.height } = window;
const { canvas.width, canvas.height } = { window.innerWidth, window.innerHeight }