Extending EcmaScript 5 Classes in ES6 Code

I want to use EcmaScript 6 (via Browserify and Babelify) in a new project, but it depends on third-party libraries written in ES5. The problem is creating subclasses in my project that extend from those in the libraries.

eg:

// Library written in ES5 function Creature(type) { this.type = type; } // my code in ES6 class Fish extends Creature { constructor(name) { super("fish"); this.name = name; } } 

This almost works, except that the Creature () constructor does not start. I developed a workaround / hack method that first creates an object of the parent class, and then adds material to it:

 class Fish extends Creature { constructor(name) { super("throw away"); //have to have this or it wont compile let obj = new Creature("fish"); obj.name = name; return obj; } } 

This approach seems to work as long as the source class does not have a constructor function.

My question is: is this the best way to extend them when using ES6 classes (except to ask the author of the library to migrate)? Or is there an even better way? I would like to continue using the syntax of class {} in my project.

+5
source share
2 answers

Your solution is working correctly using babel. Your code will compile into the following ES5 code.

 // Library written in ES5 "use strict"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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) subClass.__proto__ = superClass; } function Creature(type) { this.type = type; } // my code in ES6 var Fish = (function (_Creature) { function Fish(name) { _classCallCheck(this, Fish); _Creature.call(this, "fish"); this.name = name; } _inherits(Fish, _Creature); return Fish; })(Creature); 

As you can see from the above code, the constructor of the Creature class is called correctly. String _Creature.call(this, "fish"); .

Important link

I added the following code to demonstrate that the fish is an instance of Creature as well as an instance of Fish .

 var fish = new Fish("test"); console.log(fish.type); console.log(fish.name); console.log( fish instanceof Creature ); console.log( fish instanceof Fish); 

Output:

 fish test true true 
+2
source

ES5 constructors and ES6 classes can easily interact in the inheritance chain. If you translate your code before launching it in ES5 using tools like Babel , you can see that all this translates into Prototype-based inheritance. Take a look at this example here , which has both classes and constructor functions at three levels of the inheritance chain. Hope this helps.

0
source

All Articles