Skip to content

Instantly share code, notes, and snippets.

@Philip-Nunoo
Created August 7, 2016 16:00
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 Philip-Nunoo/db32d91cd3ef11e020ed8280cbea571d to your computer and use it in GitHub Desktop.
Save Philip-Nunoo/db32d91cd3ef11e020ed8280cbea571d to your computer and use it in GitHub Desktop.
A basic gist illustrating the use of ES6 classes compared to ES5. https://medium.com/@philipaffulnunoo/migrating-to-es6-classes-67800de0d2a2
class Car {
constructor(number_of_wheels, number_of_doors) {
this.number_of_wheels = number_of_wheels;
this.number_of_doors = number_of_doors;
}
getCarInfo() {
return this.number_of_wheels + ' wheels, ' + this.number_of_doors + ' doors.';
}
}
class Tesla extends Car{
constructor(number_of_wheels, number_of_doors, battery_size) {
super(number_of_wheels, number_of_doors)
this.battery_size = battery_size
this._weight = 100
}
getCarInfo() {
return super.getCarInfo() + " A Tesla model with " + this.battery_size + " batteries."
}
get weight () {
return this._weight
}
set weight (weight) {
this._weight = weight
}
}
let tesla = new Tesla(4,2, 5)
console.log("size", tesla.weight) // weight is 100
tesla.weight = 20
console.log("size", tesla.weight) // weight is now 20
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Car = function () {
function Car(number_of_wheels, number_of_doors) {
_classCallCheck(this, Car);
this.number_of_wheels = number_of_wheels;
this.number_of_doors = number_of_doors;
}
Car.prototype.getCarInfo = function getCarInfo() {
return this.number_of_wheels + ' wheels, ' + this.number_of_doors + ' doors.';
};
return Car;
}();
var Tesla = function (_Car) {
_inherits(Tesla, _Car);
function Tesla(number_of_wheels, number_of_doors, battery_size) {
_classCallCheck(this, Tesla);
var _this = _possibleConstructorReturn(this, _Car.call(this, number_of_wheels, number_of_doors));
_this.battery_size = battery_size;
_this._weight = 100;
return _this;
}
Tesla.prototype.getCarInfo = function getCarInfo() {
return _Car.prototype.getCarInfo.call(this) + " A Tesla model with " + this.battery_size + " batteries.";
};
_createClass(Tesla, [{
key: 'weight',
get: function get() {
return this._weight;
},
set: function set(weight) {
this._weight = weight;
}
}]);
return Tesla;
}(Car);
var tesla = new Tesla(4, 2, 5);
console.log("size", tesla.weight); // weight is 100
tesla.weight = 20;
console.log("size", tesla.weight); // weight is now 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment