0

This is my prod.js file

arr = [1,2,3,4]

function sqr(num)

{    let u = sa + arr[1]
    return u ;
}

module.exports = sqr  // line A

This is my main.js file

let sqr = require("./prod")

function main()
{
 let  z = sqr(10) + 12

  return z
}

console.log(main())

Now , in line A I am only exporting my function sqr, and that function is imported in main.js. Now in main.js when i call that function , how is it able to access arr . I read that by exporting a function , a copy of the function is created in the other file in which we require it (main.js) then how am i able to access arr. In case of closure too it can access it if it would be in same file but not sqr function is in main.js in which there is not presence of arr , so how is it able to access it ?

  • "a copy of the function is created in the other file" — No. Functions are objects. Objects are accessed by reference. You still have one function but with two references to it. – Quentin Mar 23 '23 at 10:55

0 Answers0