Search code examples
javascriptsolidityhardhat

Assigning objects to { } in Javascript and Hardhat


Consider the following code snippet:

let { networkConfig, getNetworkIdFromName } = require('../helper-hardhat-config')
const fs = require('fs')

module.exports = async ({
    getNamedAccounts,
    deployments,
    getChainId
}) => {
const { deploy, get, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = await getChainId()
let linkTokenAddress
let vrfCoordinatorAddress

if (chainId == 31337) {
    let linkToken = await get('LinkToken')
    let VRFCoordinatorMock = await get('VRFCoordinatorMock')
    linkTokenAddress = linkToken.address
    vrfCoordinatorAddress = VRFCoordinatorMock.address
    additionalMessage = " --linkaddress " + linkTokenAddress

    ...........
    ...........

In Javascript, I understand how objects are assigned to variables. eg x = {one: 1, two: 2 } but I don't understand how const { deploy, get, log } = deployments in the above code works. Just to experiment with, on the console I entered the following lines {var1,var2}={one: 1, two: 2 } but the variable var1 and var2 came as Undefined. I am just trying to understand how it is possible, for example, to use the get property as a function as in let linkToken = await get('LinkToken')

Thanks in advance !


Solution

  • It's called object destructuring, check about it here https://www.freecodecamp.org/news/javascript-object-destructuring-spread-operator-rest-parameter/