Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "export default" in JavaScript?

File: SafeString.js

// 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?

like image 971
damphat Avatar asked Jan 14 '14 15:01

damphat


People also ask

What is export default in react JS?

export default is used to export a single class, function or primitive from a script file. The export can also be written as export default class HelloWorld extends React.Component { render() { return <p>Hello, world!</

What is the difference between export and default export?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

Why export is used in JavaScript?

The export declaration is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module so they can be used by other programs with the import declaration. The value of an imported binding is subject to change in the module that exports it.

What is export default in node JS?

Default exports enable developers to export only a single class, function, variable, object, array, constant, etc. You write module. exports and assign a component that you want to export as default exports. Note: If you are interested in learning what is named exports in Node. js, then visit Node.


1 Answers

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:

// 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.

like image 167
p.s.w.g Avatar answered Oct 18 '22 19:10

p.s.w.g