It's worth using Babel's loose mode (es2015-loose in the online REPL) if you're more interested in the "old" way of doing it than in compatibility with the specs:
"use strict";
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Dog = (function() {
function Dog() {
_classCallCheck(this, Dog);
}
Dog.prototype.bark = function bark() {
console.log("Bark!");
};
return Dog;
})();
https://babeljs.io/repl/#?babili=false&browsers=&build=&buil...> For a simple es2015 it looks a bit more complicated than the prototype extension [2].
That's because it provides somewhat different features and Babel apparently attempts to replicate them properly. For instance "class" methods are non-enumerable by default, whereas "prototype" methods — being bog-standard properties — are.
Since I work with Typescript, I do the same with the TS playground, it's really usefull to learn that kind of stuff.
I sometimes see difference between TypeScript's compiler and babel.
For example in this case TypeScript doesn't care so much about `defineProperty`. I guess the rules are not so strict, since it assumes that the rest of your code will be passed through TypeScript itself.
Anyway in both cases, I prefer TypeScript's way of enhancing the class inheritance in JavaScript by introducing private / readonly and many more.
https://www.typescriptlang.org/play/index.html#src=class%20D...
For a simple es2015 it looks a bit more complicated than the prototype extension [2].
Compiles to : 1 : https://babeljs.io/repl/2 : https://babeljs.io/repl/#?babili=false&browsers=&build=&buil...