fix(curriculum): updated ES5 and ES6 class description (#47935)
* fix(curriculum): updated ES5 and ES6 class description * fix: es5 example removed * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.md Co-authored-by: Sem Bauke <semboot699@gmail.com> Co-authored-by: Jeremy L Thompson <jeremy@jeremylt.org> Co-authored-by: Sem Bauke <semboot699@gmail.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>pull/48005/head
parent
81bec2d1fb
commit
fdaa149758
|
@ -12,24 +12,35 @@ ES6 provides a new syntax to create objects, using the <dfn>class</dfn> 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.
|
||||
|
|
Loading…
Reference in New Issue