Is there a risk of a collision of the namespace of two constants if they are used in imported modules but declared outside of them?

If I have three files, basically something like this:

file_one.js:

const lol = () => {
    console.log('Laughing out loud in file_one')
}

const funcOne = () => {
    lol()
}

export default funcOne

file_two.js:

const lol = () => {
    console.log('Laughing out loud in file_two')
}

const funcTwo = () => {
    lol()
}

export default funcTwo

one_and_two_importer.js:

import funcOne from 'file_one'
import funcTwo from 'file_two'


funcOne()
funcTwo()

My assumption was that the lol function would be in the global scope and therefore would cause a namespace collision, but apparently this would not happen. Also, if I try to register the lol function in one_and_two_importer.js, I get an undefined error.

As reported, import inserts a module into the current region, but in which region are the constants used by the imported modules located?

+4
source share
1 answer

lol closures, , funcOne funcTwo, , .

ES6- ( commonJS, ) , , , , ,

import x from 'y'
import { x } from 'y'
import * as x from 'y'

​​ x. .

, , - lol , . ,

export lol

import { lol } from 'file_one'
import { lol } from 'file_two'

- , lol .

JavaScript.

+2

Source: https://habr.com/ru/post/1672226/


All Articles