2018-09-30 23:01:58 +01:00
---
id: 587d7b8b367417b2b2512b53
title: Use class Syntax to Define a Constructor Function
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301212
2021-01-13 03:31:00 +01:00
dashedName: use-class-syntax-to-define-a-constructor-function
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-11-24 21:38:30 +00:00
ES6 provides a new syntax to create objects, using the < dfn > class< / dfn > keyword.
2020-11-27 19:02:05 +01:00
It should be noted that the `class` 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.
2021-03-02 16:12:12 -08:00
In ES5, we usually define a `constructor` function and use the `new` keyword to instantiate an object.
2019-05-17 06:20:30 -07:00
```js
var SpaceShuttle = function(targetPlanet){
this.targetPlanet = targetPlanet;
}
var zeus = new SpaceShuttle('Jupiter');
```
2021-03-02 16:12:12 -08:00
The `class` syntax simply replaces the `constructor` function creation:
2019-05-17 06:20:30 -07:00
```js
class SpaceShuttle {
constructor(targetPlanet) {
this.targetPlanet = targetPlanet;
}
}
const zeus = new SpaceShuttle('Jupiter');
```
2021-03-02 16:12:12 -08:00
It should be noted that the `class` keyword declares a new function, to which a constructor is added. This constructor is invoked when `new` is called to create a new object.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
**Note:** UpperCamelCase should be used by convention for ES6 class names, as in `SpaceShuttle` used above.
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.
2020-11-27 19:02:05 +01:00
# --instructions--
2021-03-02 16:12:12 -08:00
Use the `class` keyword and write a `constructor` to create the `Vegetable` class.
2020-11-27 19:02:05 +01:00
2021-03-02 16:12:12 -08:00
The `Vegetable` class allows you to create a vegetable object with a property `name` that gets passed to the `constructor` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
`Vegetable` should be a `class` with a defined `constructor` method.
```js
assert(
typeof Vegetable === 'function' & & typeof Vegetable.constructor === 'function'
);
2018-09-30 23:01:58 +01:00
```
2021-03-02 16:12:12 -08:00
The `class` keyword should be used.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/class/g));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`Vegetable` should be able to be instantiated.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(() => {
const a = new Vegetable('apple');
return typeof a === 'object';
});
```
2019-03-25 19:49:34 +05:30
2020-11-27 19:02:05 +01:00
`carrot.name` should return `carrot` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(carrot.name == 'carrot');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code above this line
const carrot = new Vegetable('carrot');
console.log(carrot.name); // Should display 'carrot'
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-03-25 19:49:34 +05:30
class Vegetable {
constructor(name) {
this.name = name;
2019-01-24 20:36:57 +00:00
}
}
const carrot = new Vegetable('carrot');
2018-09-30 23:01:58 +01:00
```