Created
December 29, 2015 14:33
-
-
Save eshacker/eb6d147df4415ba88077 to your computer and use it in GitHub Desktop.
Explaining classes in ES 2015 via Man and SuperMan
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
"use strict"; | |
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _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; desc = parent = undefined; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | |
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 _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 Man = (function () { | |
function Man(name, powers) { | |
_classCallCheck(this, Man); | |
this.name = name; | |
this.powers = powers; | |
} | |
_createClass(Man, [{ | |
key: "defineMe", | |
value: function defineMe() { | |
console.log("I am", this.name); | |
console.log("My powers are:"); | |
this.powers.forEach(function (p) { | |
return console.log(p); | |
}); | |
} | |
}]); | |
return Man; | |
})(); | |
; | |
var SuperMan = (function (_Man) { | |
_inherits(SuperMan, _Man); | |
function SuperMan(name, powers, superpowers) { | |
_classCallCheck(this, SuperMan); | |
_get(Object.getPrototypeOf(SuperMan.prototype), "constructor", this).call(this, name, powers); | |
this.superpowers = superpowers; | |
} | |
_createClass(SuperMan, [{ | |
key: "defineMe", | |
value: function defineMe() { | |
_get(Object.getPrototypeOf(SuperMan.prototype), "defineMe", this).call(this); | |
console.log("My super powers are:"); | |
this.superpowers.forEach(function (p) { | |
return console.log(p); | |
}); | |
} | |
}]); | |
return SuperMan; | |
})(Man); | |
; | |
var me = new Man("Ecmasccript Hacker", ["Hacking", "EcmaScript"]); | |
var superman = new SuperMan("SuperMan", ["Reporting", "Wry smile"], ["Laser eyes", "X Ray vision", "Super strength"]); | |
me.defineMe(); | |
superman.defineMe(); |
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
class Man { | |
constructor(name, powers){ | |
this.name = name; | |
this.powers = powers; | |
} | |
defineMe() { | |
console.log("I am", this.name); | |
console.log("My powers are:"); | |
this.powers.forEach(p => console.log(p)); | |
} | |
}; | |
class SuperMan extends Man { | |
constructor(name, powers, superpowers){ | |
super(name, powers); | |
this.superpowers = superpowers; | |
}; | |
defineMe(){ | |
super.defineMe(); | |
console.log("My super powers are:"); | |
this.superpowers.forEach(p => console.log(p)); | |
} | |
}; | |
var me = new Man("Ecmasccript Hacker", ["Hacking", "EcmaScript"]); | |
var superman = new SuperMan("SuperMan", ["Reporting", "Wry smile"], ["Laser eyes", "X Ray vision", "Super strength"]); | |
me.defineMe(); | |
superman.defineMe(); |
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
I am Ecmasccript Hacker | |
My powers are: | |
Hacking | |
EcmaScript | |
I am SuperMan | |
My powers are: | |
Reporting | |
Wry smile | |
My super powers are: | |
Laser eyes | |
X Ray vision | |
Super strength |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment