Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

javascript - What qualifies as being a dynamic export in ES6

I hear that dynamic exports/imports are not allowed in es6.

This website Uses the example export default 5 * 7; as if it were a legal, static export. This seems reasonable since it clearly evaluates to the static value of 35, but I'm wondering what exactly qualifies as a static export now.

This Code uses export default Backbone.Router.extend({...}); as if it were a legal, static, export. This seems fishy to me as it seems like a dynamic export to me (exporting the result of a function call).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The second example only exports the result of the function call, which is static. The function is only called once, thus the result will always be the same on every import.

An Example to illustrate:

f.js

function f() {
    return 2 * Math.random();
}

export default f(); // Is called, before the export is defined. Result: 1.23543

i1.js

import f from 'f';

console.log(f); // 1.23543

i2.js

import f from 'f';

console.log(f); // 1.23543 as well

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...