Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords.chinese.md
complete-a-promise-with-resolve-and-reject.chinese.md
create-a-javascript-promise.chinese.md
create-a-module-script.chinese.md
create-an-export-fallback-with-export-default.chinese.md
create-strings-using-template-literals.chinese.md
declare-a-read-only-variable-with-the-const-keyword.chinese.md
explore-differences-between-the-var-and-let-keywords.chinese.md
handle-a-fulfilled-promise-with-then.chinese.md
handle-a-rejected-promise-with-catch.chinese.md
import-a-default-export.chinese.md
mutate-an-array-declared-with-const.chinese.md
prevent-object-mutation.chinese.md
reuse-javascript-code-using-import.chinese.md
set-default-parameters-for-your-functions.chinese.md
use--to-import-everything-from-a-file.chinese.md
use-arrow-functions-to-write-concise-anonymous-functions.chinese.md
use-class-syntax-to-define-a-constructor-function.chinese.md
use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md
use-destructuring-assignment-to-assign-variables-from-objects.chinese.md
use-destructuring-assignment-to-extract-values-from-objects.chinese.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md
use-export-to-share-a-code-block.chinese.md
use-getters-and-setters-to-control-access-to-an-object.chinese.md
use-the-rest-parameter-with-function-parameters.chinese.md
use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
write-arrow-functions-with-parameters.chinese.md
write-concise-declarative-functions-with-es6.chinese.md
write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
comment-dictionary.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.chinese.md
2020-08-04 12:43:35 +05:30

2.0 KiB

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7b8b367417b2b2512b50 Write Concise Declarative Functions with ES6 1 301224 用 ES6 编写简洁的函数声明

Description

在 ES5 中,当我们需要在对象中定义一个函数的时候,我们必须如下面这般使用function关键字:
const person = {
  name: "Taylor",
  sayHello: function() {
    return `Hello! My name is ${this.name}.`;
  }
};

在 ES6 语法的对象中定义函数的时候,你可以完全删除function关键字和冒号。请看以下例子:

const person = {
  name: "Taylor",
  sayHello() {
    return `Hello! My name is ${this.name}.`;
  }
};

Instructions

使用以上这种简短的语法,重构在bicycle对象中的setGear函数。

Tests

tests:
  - text: 不应使用<code>function</code>关键字定义方法。
    testString: getUserInput => assert(!removeJSComments(code).match(/function/));
  - text: <code>setGear</code>应是一个函数。
    testString: assert(typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/));
  - text: 执行<code>bicycle.setGear(48)</code>应可以让<code>gear</code>的值变为 48。
    testString: assert((new bicycle.setGear(48)).gear === 48);

Challenge Seed

// change code below this line
const bicycle = {
  gear: 2,
  setGear: function(newGear) {
    this.gear = newGear;
  }
};
// change code above this line
bicycle.setGear(3);
console.log(bicycle.gear);

After Test

const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');

Solution

const bicycle = {
  gear: 2,
  setGear(newGear) {
    this.gear = newGear;
  }
};
bicycle.setGear(3);