2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Use class Syntax to Define a Constructor Function
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Use class Syntax to Define a Constructor Function
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
In this lesson, you are defining the Vegetable object using class syntax.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## 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.
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 2
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
Put a constructor with a parameter called `name`, and set it to `this.name`.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07: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 */
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
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();
|
2019-07-24 00:59:27 -07:00
|
|
|
const carrot = new Vegetable("carrot");
|
2018-10-12 15:37:13 -04:00
|
|
|
console.log(carrot.name); // => should be 'carrot'
|
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|