freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../es6/use--to-import-everything-f...

1.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b8c367417b2b2512b57 Use * to Import Everything from a File 1 使用*从文件导入所有内容

Description

假设您有一个文件要将其所有内容导入当前文件。这可以使用import *语法完成。这是一个示例,其中名为"math_functions"的文件的内容被导入到同一目录中的文件中:
从“math_functions”导入*作为myMathModule;
myMathModule.add2,3;
myMathModule.subtract5,3;
并打破代码:
从“file_path_goes_here”导入* as object_with_name_of_your_choice
object_with_name_of_your_choice.imported_function
您可以使用import * as后面的任何名称import * as语句的一部分。为了使用此方法,它需要一个接收导入值的对象。从这里,您将使用点表示法来调用导入的值。

Instructions

下面的代码需要在导入的同一目录中找到的文件"capitalize_strings"的内容。使用提供的对象将相应的import *语句添加到文件的顶部。

Tests

tests:
  - text: 正确使用<code>import * as</code>语法。
    testString: 'assert(code.match(/import\s+\*\s+as\s+[a-zA-Z0-9_$]+\s+from\s*"\s*capitalize_strings\s*"\s*;/gi), "Properly uses <code>import * as</code> syntax.");'

Challenge Seed

"use strict";

Before Test

window.require = function(str) {
if (str === 'capitalize_strings') {
return {
capitalize: str => str.toUpperCase(),
lowercase: str => str.toLowerCase()
}}};

Solution

// solution required