在 javascript 中,babel 将带有构造函数的 es6 类编译为 es5 函数时,核心机制是调用 classcallcheck 检查实例合法性,并使用 _createclass 辅助函数定义构造函数原型和静态方法。这种转换确保了类不能被直接作为普通函数调用,同时保留了继承链和方法定义的语义。

Babel 如何处理类的构造函数定义?
Babel 通过生成一个名为 _createClass 的内部辅助函数来处理类的定义。该函数接收构造函数、实例方法和静态方法作为参数,使用 Object.defineProperty 将方法定义到原型上,并设置 enumerable: false 以防止枚举。对于构造函数本身,它会返回一个经过包装的函数,确保在使用 new 关键字调用时执行正确的初始化逻辑。

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function Person(name) {
_classCallCheck(this, Person);
this.name = name;
}
Person = _createClass(Person, [{
key: "greet",
value: function greet() {
return "Hello, " + this.name;
}
}]);
子类继承中的 super 是如何处理的?
当处理子类继承时,Babel 会生成 _inherits 辅助函数来建立原型链。它首先将子类的原型设置为父类原型的副本,然后修正子类的 constructor 属性指向子类自身。在子类构造函数内部,Babel 会插入对 _getPrototypeOf 和 _setPrototypeOf 的调用,以确保 super 能够正确访问父类的方法。此外,_possibleConstructorReturn 用于处理构造函数返回值不是对象时的情况。
立即学习“Java免费学习笔记(深入)”;

function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
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 _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 _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, proto) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, proto) {
o.__proto__ = proto;
return o;
};
return _setPrototypeOf(o, proto);
}
function Animal(name) {
_classCallCheck(this, Animal);
this.name = name;
}
Animal = _createClass(Animal, [{
key: "speak",
value: function speak() {
return this.name + " makes a noise.";
}
}]);
function Dog(name) {
_classCallCheck(this, Dog);
var _this;
_this = _possibleConstructorReturn(this, (Dog.__proto__ || Object.getPrototypeOf(Dog)).call(this, name));
return _possibleConstructorReturn(_this);
}
_inherits(Dog, Animal);
Dog = _createClass(Dog, [{
key: "speak",
value: function speak() {
return _getPrototypeOf(Dog.prototype).speak.call(this) + " Barks.";
}
}]);
如果构造函数中没有显式调用 super() 会发生什么?
在 ES6 类中,子类的构造函数必须在访问 this 之前调用 super()。如果未调用,JavaScript 引擎会抛出 ReferenceError。Babel 编译后的代码中,_possibleConstructorReturn 函数会检查 self 是否为空,如果为空则抛出错误。这确保了子类实例在初始化过程中始终处于有效状态。例如,在上面的 Dog 类中,如果省略了 super(name) 调用,_possibleConstructorReturn 将检测到 self 未初始化并抛出异常。


















