I have been looking at the following article https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import . There is a code snippet in the examples:
file.js
function getJSON(url, callback) {
let xhr = new XMLHttpRequest();
xhr.onload = function () {
callback(this.responseText)
};
xhr.open('GET', url, true);
xhr.send();
}
export function getUsefulContents(url, callback) {
getJSON(url, data => callback(JSON.parse(data)));
}
main.js
import { getUsefulContents } from '/modules/file.js';
getUsefulContents('http://www.example.com',
data => { doSomethingUseful(data); });
getUsefulContents
is exported in file.js
and later imported in main.js
. But getJSON
is not exported. Intuitively it seems to me that since the function getJSON
is not exported to main.js
and getUsefulContents
depends on getJSON
there should be an error. But how is it that inspite of importing only one function from file.js
, the dependent function also works in the scope of main.js
. Is there something I should know about scoping in case of exports or any other concepts? Any clarification would help, thanks.