patternjavascriptCritical
What is "export default" in JavaScript?
Viewed 0 times
exportwhatjavascriptdefault
Problem
File: SafeString.js
I have never seen
// Build out our basic SafeString type
function SafeString(string) {
this.string = string;
}
SafeString.prototype.toString = function() {
return "" + this.string;
};
export default SafeString;I have never seen
export default before. Are there any equivalent stuff for export default that can be easier to understand?Solution
It's part of the ES6 module system, described here. There is a helpful example in that documentation, also:
If a module defines a default export:
then you can import that default export by omitting the curly braces:
Update: As of June 2015, the module system is defined in §15.2 and the
If a module defines a default export:
// foo.js
export default function() { console.log("hello!") }then you can import that default export by omitting the curly braces:
import foo from "foo";
foo(); // hello!Update: As of June 2015, the module system is defined in §15.2 and the
export syntax in particular is defined in §15.2.3 of the ECMAScript 2015 specification.Code Snippets
// foo.js
export default function() { console.log("hello!") }import foo from "foo";
foo(); // hello!Context
Stack Overflow Q#21117160, score: 708
Revisions (0)
No revisions yet.