freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions.chinese.md
Kristofer Koishigawa b3213fc892 fix(i18n): chinese test suite (#38220)
* fix: Chinese test suite

Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working.

* fix: ran script, updated testStrings and solutions
2020-03-03 18:49:47 +05:30

2.4 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b87367417b2b2512b43 Use Arrow Functions to Write Concise Anonymous Functions 1 使用箭头函数编写简明的匿名函数

Description

在JavaScript中我们通常不需要命名我们的函数特别是在将函数作为参数传递给另一个函数时。相反我们创建内联函数。我们不需要命名这些函数因为我们不会在其他任何地方重用它们。为此我们经常使用以下语法
const myFunc = function{
const myVar =“value”;
返回myVar;
}
ES6为我们提供了语法糖而不必以这种方式编写匿名函数。相反您可以使用箭头函数语法
const myFunc ==> {
const myVar =“value”;
返回myVar;
}
当没有函数体,并且只有返回值时,箭头函数语法允许您省略关键字return以及代码周围的括号。这有助于将较小的函数简化为单行语句:
const myFunc ==>“value”
默认情况下,此代码仍将返回value

Instructions

重写分配给变量magic的函数,该函数返回一个新的Date()以使用箭头函数语法。还要确保使用关键字var定义任何内容。

Tests

tests:
  - text: 用户确实替换了<code>var</code>关键字。
    testString: getUserInput => assert(!getUserInput('index').match(/var/g));
  - text: <code>magic</code>应该是一个常量变量(通过使用<code>const</code> )。
    testString: getUserInput => assert(getUserInput('index').match(/const\s+magic/g));
  - text: <code>magic</code>是一种<code>function</code> 。
    testString: assert(typeof magic === 'function');
  - text: <code>magic()</code>返回正确的日期。
    testString: assert(magic().setHours(0,0,0,0) === new Date().setHours(0,0,0,0));
  - text: <code>function</code>关键字未使用。
    testString: getUserInput => assert(!getUserInput('index').match(/function/g));

Challenge Seed

var magic = function() {
  "use strict";
  return new Date();
};

Solution

// solution required