freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../es6/use-class-syntax-to-define-.../index.md

60 lines
1.2 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Use class Syntax to Define a Constructor Function
---
## Use class Syntax to Define a Constructor Function
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
In this lesson, you are defining the Vegetable object using class syntax.
## Hint 1:
Create the class called `Vegetable`. It will contain the necessary details about the `Vegetable` object.
## Hint 2:
Put a constructor with a parameter called `name`, and set it to `this.name`.
## Spoiler Alert - Solution Ahead!
## Solution:
```javascript
function makeClass() {
"use strict";
/* Alter code below this line */
class Vegetable {
constructor(name){
this.name = name;
}
}
/* Alter code above this line */
return Vegetable;
}
```
=======
Spoiler Warning: here is a basic solution to this challenge in case you're stuck.
```javascript
function makeClass() {
"use strict";
/* Alter code below this line */
class Vegetable {
constructor(Vegetable){
this.Vegetable = Vegetable;
}
}
/* Alter code above this line */
return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'
```