Skip to content

Instantly share code, notes, and snippets.

@balazsnemeth
Last active April 9, 2018 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balazsnemeth/b355fcaa958fde2521749e76694a4944 to your computer and use it in GitHub Desktop.
Save balazsnemeth/b355fcaa958fde2521749e76694a4944 to your computer and use it in GitHub Desktop.
How to refactor an object from ES5 to ES6
### 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