Files

49 lines
887 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use class Syntax to Define a Constructor Function
---
# Use class Syntax to Define a Constructor Function
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
In this lesson, you are defining the Vegetable object using class syntax.
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
Create the class called `Vegetable`. It will contain the necessary details about the `Vegetable` object.
### Hint 2
2018-10-12 15:37:13 -04:00
Put a constructor with a parameter called `name`, and set it to `this.name`.
---
## Solutions
2018-10-12 15:37:13 -04:00
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function makeClass() {
"use strict";
/* Alter code below this line */
class Vegetable {
constructor(name) {
this.name = name;
}
}
2018-10-12 15:37:13 -04:00
/* Alter code above this line */
return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable("carrot");
2018-10-12 15:37:13 -04:00
console.log(carrot.name); // => should be 'carrot'
```
</details>