From the website exploringjs
Before ES6, there was no corresponding mechanism for extracting data. That’s what destructuring is – it lets you extract multiple properties from an object via an object pattern. For example, on the left-hand side of an assignment:
const { first: f, last: l } = obj;
And I understand the sample below, for example, one would be assigning the createServer
method from the http
module to a variable of the same name.
const { createServer } = require('http');
But what about this:
const { parse: parseUrl } = require('url');
How do you use this in your code?
You use this when you want to change the name of the variable coming from require('url')
because require('url') contain parse
but lets say you already have variable called parse
in your current scope and you want to have parse
from require('url')
in this case you use this pattern to rename it to parseUrl
Example:
const parse = "some value";
const { parse: parseUrl } = require('url');
console.log(parse); // "some value";
console.log(parseUrl); // value coming from require('url');