diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md index 8b847ad5fea..ecb199d45f4 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md @@ -12,24 +12,35 @@ ES6 provides a new syntax to create objects, using the class keyword. 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. -In ES5, we usually define a `constructor` function and use the `new` keyword to instantiate an object. - -```js -var SpaceShuttle = function(targetPlanet){ - this.targetPlanet = targetPlanet; -} -var zeus = new SpaceShuttle('Jupiter'); -``` - -The `class` syntax simply replaces the `constructor` function creation: +In ES5, an object can be created by defining a `constructor` function and using the `new` keyword to instantiate the object. + +In ES6, a `class` declaration has a `constructor` method that is invoked with the `new` keyword. If the `constructor` method is not explicitly defined, then it is implicitly defined with no arguments. ```js +// Explicit constructor class SpaceShuttle { constructor(targetPlanet) { this.targetPlanet = targetPlanet; } + takeOff() { + console.log("To " + this.targetPlanet + "!"); + } } + +// Implicit constructor +class Rocket { + launch() { + console.log("To the moon!"); + } +} + const zeus = new SpaceShuttle('Jupiter'); +// prints To Jupiter! in console +zeus.takeOff(); + +const atlas = new Rocket(); +// prints To the moon! in console +atlas.launch(); ``` 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.