Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
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.english.md
complete-a-promise-with-resolve-and-reject.english.md
create-a-javascript-promise.english.md
create-a-module-script.english.md
create-an-export-fallback-with-export-default.english.md
create-strings-using-template-literals.english.md
declare-a-read-only-variable-with-the-const-keyword.english.md
explore-differences-between-the-var-and-let-keywords.english.md
handle-a-fulfilled-promise-with-then.english.md
handle-a-rejected-promise-with-catch.english.md
import-a-default-export.english.md
mutate-an-array-declared-with-const.english.md
prevent-object-mutation.english.md
reuse-javascript-code-using-import.english.md
set-default-parameters-for-your-functions.english.md
use--to-import-everything-from-a-file.english.md
use-arrow-functions-to-write-concise-anonymous-functions.english.md
use-class-syntax-to-define-a-constructor-function.english.md
use-destructuring-assignment-to-assign-variables-from-arrays.english.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.english.md
use-destructuring-assignment-to-assign-variables-from-objects.english.md
use-destructuring-assignment-to-extract-values-from-objects.english.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.english.md
use-export-to-share-a-code-block.english.md
use-getters-and-setters-to-control-access-to-an-object.english.md
use-the-rest-parameter-with-function-parameters.english.md
use-the-spread-operator-to-evaluate-arrays-in-place.english.md
write-arrow-functions-with-parameters.english.md
write-concise-declarative-functions-with-es6.english.md
write-concise-object-literal-declarations-using-object-property-shorthand.english.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
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/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.english.md

95 lines
2.9 KiB
Markdown
Raw Normal View History

---
id: 587d7b8b367417b2b2512b53
title: Use class Syntax to Define a Constructor Function
challengeType: 1
forumTopicId: 301212
---
## Description
<section id='description'>
ES6 provides a new syntax to create objects, using the <dfn>class</dfn> keyword.
It should be noted that the <code>class</code> syntax is just syntax, and not a full-fledged class-based implementation of an object-oriented paradigm, unlike in languages such as Java, Python, Ruby, etc.
In ES5, we usually define a constructor function and use the <code>new</code> keyword to instantiate an object.
```js
var SpaceShuttle = function(targetPlanet){
this.targetPlanet = targetPlanet;
}
var zeus = new SpaceShuttle('Jupiter');
```
The <code>class</code> syntax simply replaces the constructor function creation:
```js
class SpaceShuttle {
constructor(targetPlanet) {
this.targetPlanet = targetPlanet;
}
}
const zeus = new SpaceShuttle('Jupiter');
```
It should be noted that the <code>class</code> keyword declares a new function, to which a constructor is added. This constructor is invoked when <code>new</code> is called to create a new object.<br>
<strong>Notes:</strong><br><ul>
<li> UpperCamelCase should be used by convention for ES6 class names, as in <code>SpaceShuttle</code> used above.</li>
<li> The constructor method is a special method for creating and initializing an object created with a class. You will learn more about it in the Object Oriented Programming section of the JavaScript Algorithms And Data Structures Certification.</li></ul>
</section>
## Instructions
<section id='instructions'>
Use the <code>class</code> keyword and write a constructor to create the <code>Vegetable</code> class.
The <code>Vegetable</code> class allows you to create a vegetable object with a property <code>name</code> that gets passed to the constructor.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>Vegetable</code> should be a <code>class</code> with a defined <code>constructor</code> method.
testString: assert(typeof Vegetable === 'function' && typeof Vegetable.constructor === 'function');
- text: <code>class</code> keyword should be used.
testString: assert(code.match(/class/g));
- text: <code>Vegetable</code> should be able to be instantiated.
testString: assert(() => {const a = new Vegetable("apple"); return typeof a === 'object';});
- text: <code>carrot.name</code> should return <code>carrot</code>.
testString: assert(carrot.name=='carrot');
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Only change code below this line
// Only change code above this line
const carrot = new Vegetable('carrot');
console.log(carrot.name); // Should display 'carrot'
```
</div>
</section>
## Solution
<section id='solution'>
```js
class Vegetable {
constructor(name) {
this.name = name;
2019-01-24 20:36:57 +00:00
}
}
const carrot = new Vegetable('carrot');
```
</section>