Skip to content

Instantly share code, notes, and snippets.

@csarrazi
Created August 7, 2015 09:28
Show Gist options
  • Save csarrazi/53f0d44f54cae0873728 to your computer and use it in GitHub Desktop.
Save csarrazi/53f0d44f54cae0873728 to your computer and use it in GitHub Desktop.
ES6 classes
class Animal {
constructor(race) {
this.race = race;
}
}
class Cat extends Animal {
constructor(race) {
super(race);
}
say() {
console.log(this.race + ' says "Meow!"');
}
}
class Dog extends Animal {
constructor(race) {
super(race);
}
say() {
console.log(this.race + ' says "Bark!"');
}
}
var cat = new Cat('cheshire')
, dog = new Dog('bulldog')
;
cat.say();
dog.say();
console.log(cat instanceof Cat);
console.log(cat instanceof Dog);
console.log(cat instanceof Animal);
console.log(dog instanceof Animal);
'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; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
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 Animal = function Animal(race) {
_classCallCheck(this, Animal);
this.race = race;
};
var Cat = (function (_Animal) {
_inherits(Cat, _Animal);
function Cat(race) {
_classCallCheck(this, Cat);
_get(Object.getPrototypeOf(Cat.prototype), 'constructor', this).call(this, race);
}
_createClass(Cat, [{
key: 'say',
value: function say() {
console.log(this.race + ' says "Meow!"');
}
}]);
return Cat;
})(Animal);
var Dog = (function (_Animal2) {
_inherits(Dog, _Animal2);
function Dog(race) {
_classCallCheck(this, Dog);
_get(Object.getPrototypeOf(Dog.prototype), 'constructor', this).call(this, race);
}
_createClass(Dog, [{
key: 'say',
value: function say() {
console.log(this.race + ' says "Bark!"');
}
}]);
return Dog;
})(Animal);
var cat = new Cat('cheshire'),
dog = new Dog('bulldog');
cat.say();
dog.say();
console.log(cat instanceof Cat);
console.log(cat instanceof Dog);
console.log(cat instanceof Animal);
console.log(dog instanceof Animal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment