Babeljs doesn't translate extended classes properly

I have an example code from MDN (slightly modified to display the results):

"use strict"; class myDate extends Date { constructor() { super(); } getFormattedDate() { var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear(); } } var testDate = new myDate(); document.write(testDate.getFormattedDate()); 

It works in Google Chrome browser and host5, but when I transfer it using Babeljs to ES5 (ES2015 pre-release kit), it does not work:

 "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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } var myDate = function (_Date) { _inherits(myDate, _Date); function myDate() { _classCallCheck(this, myDate); return _possibleConstructorReturn(this, Object.getPrototypeOf(myDate).call(this)); } _createClass(myDate, [{ key: 'getFormattedDate', value: function getFormattedDate() { var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear(); } }]); return myDate; }(Date); var testDate = new myDate(); document.write(testDate.getFormattedDate()); 

Why?

I found the answer in babel docs :

Partial support
The built-in subclassification must be evaluated on a case-by-case basis, since classes such as HTMLElement can be subclassed, while many of them, such as Date, Array and Error, cannot be bound by ES5 restrictions.

I would appreciate if the granny made a mistake seeing the internal extensions of objects (which he does not support), instead of pretending that everything is rosy and creates a faulty code: /

+2
source share
1 answer

According to the ECMAScript 6 compatibility table , BabelJS does not support class extensions.

+2
source

Source: https://habr.com/ru/post/1013679/


All Articles