Javascript Es6 default export

2019-07-20 21:14发布

问题:

I just want to double check since I am trying to do this ES6 export default:

var Member = function(){}
export {
    Member as default
 };

JShint is error-ing out with this syntax (es6 enabled) but I thought it was valid. Is this really invalid or what is a valid way for writing a default export with the export syntax as

 export {
    Member 
 };

I was using this as reference: http://www.2ality.com/2014/09/es6-modules-final.html The example they gave was:

//------ module1.js ------
export default 123;
//------ module2.js ------
const D = 123;
export { D as default };

Why is this module2's 2nd line valid? (or is it?)

回答1:

(As so often) this is is jshint's fault. The line is indeed valid, ES6 Export syntax does permit the use of any IdentifierName - which includes keywords such as default - for the exported name of an ExportClause.

I would however discourage from using it. Default exports are much easier to write and read in the export default notation, such as

var Member = function() {};
export default Member;
// or
export default function Member() {}

Admittedly export default Member; is not exactly equivalent to export { Member as default } but unless you are try to reassign it (you're not, right?) it doesn't make a difference.