Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 export default function

can I export more than one function per file ? it seems like when I do that , the second function ovverides the first one ,

example : in my index.js file :

export default function aFnt(){
    console.log("function a");
}
export default function bFnt(){
    console.log("function b");
}

then when I import it in my file :

import aFnt from "./index";

console.log("aFnt : ",aFnt);

the result of the console.log is bFnt

what exactly is the case here ? do I have to create a new file per function ? that is not very practical , any solution or workaround ?

like image 439
sisimh Avatar asked Feb 21 '16 20:02

sisimh


People also ask

What is export default in es6?

Export Default is used to export only one value from a file which can be a class, function, or object. The default export can be imported with any name.

What is the use of export default?

What is export default used for? Export defaults are used to export a single module, variable, expression, or function from a JavaScript file so that it can be used in any other file of either the same program or even in an entirely different program.

How do I make JavaScript default export?

return "This a default export." export { fun as default , x, y, square }; While importing this module. js we can use any name for fun because it is a default export and curly braces for other named exports.

Can you export default multiple functions?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

What is ES6 import and export in JavaScript?

ES6 | Import and Export. The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import.

How to create modules in ES6?

The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

What is the default export for a module?

Default Export: (export default) Concerning the default export, there is only a single default export per module. A default export can be a function, a class, an object or anything else. This value is to be considered as the “main” exported value since it will be the simplest to import.

What is a default export value?

A default export can be a function, a class, an object or anything else. This value is to be considered as the “main” exported value since it will be the simplest to import.


2 Answers

madox2's answer totally works if you want to import named functions.

If you still want to import the default, there's another technique:

function a() {}

function b() {}

export default { a, b }

and when you import:

import myObject from './index.js';

myObject.a(); // function a
myObject.b(); // function b

I hope this helps!

like image 175
Bobby Matson Avatar answered Oct 22 '22 17:10

Bobby Matson


You can use named export instead of default:

export function aFnt(){
    console.log("function a");
}
export function bFnt(){
    console.log("function b");
}

and import it like:

import {aFnt, bFnt} from "./index";
like image 38
madox2 Avatar answered Oct 22 '22 16:10

madox2