Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6 Transpiler中的导入/导出_Javascript_Google Chrome_Ecmascript 6_Transpiler - Fatal编程技术网

Javascript ES6 Transpiler中的导入/导出

Javascript ES6 Transpiler中的导入/导出,javascript,google-chrome,ecmascript-6,transpiler,Javascript,Google Chrome,Ecmascript 6,Transpiler,这不是以下问题的副本,这些问题涉及特定于浏览器的问题。我希望得到一个答案,import/export是否能在客户端工作 //lib.js 导出常量sqrt=Math.sqrt; 导出功能方块(x){ 返回x*x; } 导出功能图(x,y){ 返回sqrt(平方(x)+平方(y)); } //main.js “严格使用”; 从'lib'导入{square,diag}; 控制台。日志(方形(11));//121 console.log(diag(4,3));//5 进口支票 说 注意:目前没

这不是以下问题的副本,这些问题涉及特定于浏览器的问题。我希望得到一个答案,
import/export
是否能在客户端工作

  • //lib.js
    导出常量sqrt=Math.sqrt;
    导出功能方块(x){
    返回x*x;
    }
    导出功能图(x,y){
    返回sqrt(平方(x)+平方(y));
    }
    //main.js
    “严格使用”;
    从'lib'导入{square,diag};
    控制台。日志(方形(11));//121
    console.log(diag(4,3));//5
    
    进口支票
    

    注意:目前没有任何浏览器本机实现此功能。它在许多Transpiler中实现,如Traceur编译器、Babel或Rollup

    例如,在代码段上使用babel后,您将得到如下结果:

    //lib.js
    “严格使用”;
    Object.defineProperty(导出,“\uu esModule”{
    值:true
    });
    exports.square=square;
    exports.diag=diag;
    var sqrt=Math.sqrt;
    exports.sqrt=sqrt;
    函数平方(x){
    返回x*x;
    }
    功能诊断(x,y){
    返回sqrt(平方(x)+平方(y));
    }
    //------main.js------
    "严格使用",;
    var_lib=require('lib');
    console.log((0,_lib.square)(11));//121
    
    console.log((0,_lib.diag)(4,3));//5
    就像马纳索夫·丹尼尔说的那样

    注意:目前没有任何浏览器本机实现此功能。它在许多Transpiler中实现,如Traceur编译器、Babel或Rollup

    对于较新版本的ECMAScript(在浏览器中实现之前),通常需要将ECMAScript代码转换(编译)为JavaScript。我选择的工具是巴别塔,尽管还有很多其他工具

    您可以通过转到终端并输入以下内容来安装Babel CLI:

    $ npm install --save-dev babel-cli
    

    每次您对ES进行更改时,都应该重新编译到JS。

    您的意思是说,如果没有babel、traceur、typescript等Transpiler,即使我们添加了use strict
    “use strict”,也无法进行测试
    定义JavaScript代码应在“严格模式”下执行。我希望答案作为工作代码片段,使导入功能工作,而不仅仅是引用@我使用了babel,得到了我添加到答案中的代码。@Venkatraman还研究了这个问题,如果我们给import语句一行,babelJS到底做了什么。这会把它转换成需要吗?是的。ES6 Transipler将始终将ES6转换为其最新版本的JavaScript对应方。如果您的问题是关于chrome的,请共享它以更好地理解为什么您的标题是“在ES6 Transipler中导入/导出”?如果您认为问题不重复,请编辑您的问题。@torazaburo,我已经更新了问题,这是我所期望的!先前标记的原始问题和当前问题是相同的。它需要重新开放@torazaburo,请重新讨论问题,重新开始