ecmascript5中引入了export关键字:
var myFunc1 = function() { console.log('hello'); };
export.myFunc1 = myFunc1;
如果我在firefox控制台中运行以上代码,则会出现错误:
SyntaxError: missing declaration after 'export' keyword
export.myFunc1 = myFunc1;
我不明白我需要宣布什么。
我使用方式有误吗?
任何建议都很好!
最佳答案
The syntax for ES6 export
looks like this:
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
//------ main.js ------
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
请注意,这与Node.js中使用的CommonJS
modules.export
语法不同。关于javascript - 如何在浏览器或控制台中使用javascript export关键字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30141209/