Last active
April 9, 2018 14:20
-
-
Save balazsnemeth/b355fcaa958fde2521749e76694a4944 to your computer and use it in GitHub Desktop.
How to refactor an object from ES5 to ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### ES5: | |
var Rectangle = function (w, h) { | |
this.width = w; | |
this.height = h; | |
this.area = function() { | |
return calcArea(); | |
} | |
var calcArea = function() { | |
return this.height * this.width; | |
} | |
} | |
### ES6: | |
const calcArea = () => { | |
return this.height * this.width; | |
} | |
class Rectangle { | |
constructor(w, h) { | |
this.width = w; | |
this.height = h; | |
} | |
area() { | |
return calcArea(); | |
} | |
} | |
### Usage | |
var r = new Rectangle(10, 40); | |
console.log(r.width) // 10; | |
console.log(r.area()) // 400; | |
console.log(r.calcArea()) // Error | |
webpack can translate from ES6 to ES5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment