2.4 KiB
2.4 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b8c367417b2b2512b56 | Use export to Reuse a Code Block | 1 |
Description
import
and how it can be leveraged to import small amounts of code from large files. In order for this to work, though, we must utilize one of the statements that goes with import
, known as export. When we want some code - a function, or a variable - to be usable in another file, we must export it in order to import it into another file. Like import
, export
is a non-browser feature.
The following is what we refer to as a named export. With this, we can import any code we export into another file with the import
syntax you learned in the last lesson. Here's an example:
const capitalizeString = (string) => {Alternatively, if you would like to compact all your
return string.charAt(0).toUpperCase() + string.slice(1);
}
export { capitalizeString } //How to export functions.
export const foo = "bar"; //How to export variables.
export
statements into one line, you can take this approach:
const capitalizeString = (string) => {Either approach is perfectly acceptable.
return string.charAt(0).toUpperCase() + string.slice(1);
}
const foo = "bar";
export { capitalizeString, foo }
Instructions
export
, export the two variables.
Tests
tests:
- text: <code>foo</code> is exported.
testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+foo\s*=\s*"bar"/g), '<code>foo</code> is exported.');
- text: <code>bar</code> is exported.
testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+bar\s*=\s*"foo"/g), '<code>bar</code> is exported.');
Challenge Seed
"use strict";
const foo = "bar";
const bar = "foo";
Before Test
self.exports = function(){};
Solution
"use strict";
export const foo = "bar";
export const bar = "foo";