简体   繁体   English

由于未进行类吊装,Babel转译的ES2015中的继承无法正常工作

[英]Inheritance in Babel transpiled ES2015 not working due to no class hoisting

I am developing a web app and have my Javascript broken into multiple files. 我正在开发一个Web应用程序,并且将Javascript分解为多个文件。 I am using Babel to transpile the directory of ES2015 source files to a single ES5 file. 我正在使用Babel将ES2015源文件的目录转换为单个ES5文件。 Coming from an objected oriented background I love having "classes" whether they are just syntactic sugar or not. 来自面向对象的背景,我喜欢拥有“类”,无论它们是否只是语法糖。

I recently created a base class in a file and then tried extending it in a different class in a different file. 我最近在文件中创建了一个基类,然后尝试将其扩展到另一个文件中的另一个类中。 What I quickly realized is that class definitions are not hoisted and when Babel combined the multiple files the order is such that the base class in not defined yet when I instantiate the inherited class (so it throws a runtime error). 我很快意识到,没有悬挂类定义,并且当Babel合并多个文件时,顺序是这样的:当我实例化继承的类时,还没有定义基类(因此会引发运行时错误)。

Here is a general example that illustrates what I am talking about: 这是一个一般示例,说明了我在说什么:

base.js: base.js:

class Base {
    constructor() {
        console.log("BASE");
    }
}

derived.js: 派生.js:

class Derived extends Base {
    constructor() {
        super();
        console.log("DERIVED");
    }
}

main.js: main.js:

var instance = new Derived();

Babel-CLI: Babel-CLI:

babel src --out-file index.js

results in index.js looking like: 结果为index.js看起来像:

class Base {
    constructor() {
        console.log("BASE");
    }
}
class Derived extends Base {
    constructor() {
        super();
        console.log("DERIVED");
    }
}
var instance = new Derived();

This works fine. 这很好。 However, if I were to change the filename of base.js to xbase.js then index.js ends up looking like this: 但是,如果我要将base.js的文件名更改为xbase.js,则index.js最终看起来像这样:

class Derived extends Base {
    constructor() {
        super();
        console.log("DERIVED");
    }
}
var instance = new Derived();
class Base {
    constructor() {
        console.log("BASE");
    }
}

this fails because the Base class is not defined before it is used: 这将失败,因为在使用基类之前未定义基类:

index.js:1 Uncaught ReferenceError: Base is not defined

So clearly when Babel concatenates the files it grabs the file names alphabetically and sticks them together that way. 很明显,当Babel连接文件时,它会按字母顺序获取文件名并将它们以这种方式粘贴在一起。 How do I make this work without making sure my file names are named according to the order I want them to appear in the final file? 如何在不确保文件名按照希望它们出现在最终文件中的顺序命名的情况下进行此工作? Does using modules solve this problem? 使用模块是否可以解决此问题? Is it the ONLY way to solve this problem? 这是解决这个问题的唯一方法吗?

Make sure to import and export your files. 确保导入导出文件。 That way, babel won't have to rely on file load order during concat of the files. 这样,babel不必在文件合并期间依赖文件加载顺序。

Example: 例:

base.js base.js

class Base {
    constructor() {
        console.log("BASE");
    }
}

export default Base;

derived.js 派生.js

import Base from './base.js';

class Derived extends Base {
    constructor() {
        super();
        console.log("DERIVED");
    }
}

export default Derived;

main.js main.js

import Derived from './derived.js'

var instance = new Derived();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM