JS-Dev-101 practice question 8 of 25

A developer wants to use a module named universalContainerslib and then call functions from it. How should a developer import every function from the module and then call the functions foo and bar?

Full set: 148 questions
Answer options
A: import * as lib from '/path/universalContainerslib.js'; lib.foo(); lib.bar(); Correct answer
B: import * from '/path/universalContainerslib.js'; universalContainerslib.foo(); universalContainerslib.bar();
C: import all from '/path/universalContainerslib.js'; universalContainerslib.foo(); universalContainerslib.bar();
D: import {foo, bar} from '/path/universalContainerslib.js'; foo(); bar();
Correct Answer:
A. import * as lib from '/path/universalContainerslib.js'; lib.foo(); lib.bar();
Explanation

import as alias from "module" gathers every named export into a single namespace object (alias). Functions foo and bar are accessed as properties (alias.foo(), alias.bar()).

Why Incorrect

B. import syntax requires “as ”; without it the statement is invalid.
C. “all” is not valid ES6 import syntax; compilation error.
D. Destructuring import brings in only foo and bar, not every function as required.

References

1. ECMA-262, 13th ed., §15.2.2.14 “ImportDeclaration”, Grammar: import as IdentifierName from ModuleSpecifier.

2. Salesforce Lightning Web Components Dev Guide, “ES Modules”, section “Namespace imports” (alias.foo()).

Scroll to Top