What is the logic behind the variables put in {}/extrapolated variables? #313
-
As I get further into the tutorial, I keep getting confused about the "extrapolation" of different variables. I understand what it does but am wondering what the logical reasoning behind this syntactical sugar is. Also, what is this method of extrapolation called? Here is some of the confusing code: module.exports = async ({ getNamedAccounts, deployments }),
const { networkConfig } = require("../helper-hardhat-config"),
const { network } = require("hardhat"),
const { deployer } = await getNamedAccounts. basically, everything that uses {} when declaring a variable/parameter. Thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is actually called Object Destructuring in javascript(sorry, I think I called it extrapolation). Example 1On this line: module.exports = async ({ getNamedAccounts, deployments }), When we call module.exports = async (hre) { Now, the module.exports = async (hre) {
{ getNamedAccounts, deployments } = hre Javascript actually just allows us to do it right in the parameters though, like so: module.exports = async ({ getNamedAccounts, deployments }){ Example 2For const { networkConfig } = require("../helper-hardhat-config") We have a variable named const helperHardhatConfig = require("../helper-hardhat-config")
const networkConfig = helperHardhatConfig.networkConfig or const helperHardhatConfig = require("../helper-hardhat-config")
const {networkConfig} = helperHardhatConfig or const {networkConfig} = require("../helper-hardhat-config") And then it's the same idea for the other examples. |
Beta Was this translation helpful? Give feedback.
This is actually called Object Destructuring in javascript(sorry, I think I called it extrapolation).
Example 1
On this line:
When we call
yarn hardhat deploy
, we actually pass anhre
or "hardhat runtime environment" variable to theasync
function described here. So you could think of it as doing:Now, the
hre
object has 2 objects we care about, namelygetNamedAccounts
anddeployments
. Since we know we only want those, we can just pull them out of hre, or "destructure" them like so:Javascript actually just allows us to do it r…