Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "=>" mean in javascript when used with export default? [duplicate]

I am analyzing some angular js source code of angular-file-upload plugin and I have some problems trying to understand some code.

I know that export is part of the new ES6 standards and it used to export functions and objects from a given file (or module).

But the following syntax is a bit weird me :

let {
    copy,
    extend,
    forEach,
    isObject,
    isNumber,
    isDefined,
    isArray,
    element
    } = angular;


    export default (fileUploaderOptions, $rootScope, $http, $window, 
                      FileLikeObject, FileItem) => {


        let {
            File,
            FormData
            } = $window;


        class FileUploader { 

          // class implemention.... 
        }

        return FileUploader;
    }

What is the use of the => operator in this statement?

like image 349
KAD Avatar asked Sep 16 '15 20:09

KAD


People also ask

What is a default export in JavaScript?

Export default is used when there is only one export to be made from a particular file and when importing this one element, we don't have to worry about giving the same name to our import. This combination of export and import allows us to implement modularity.

Can we default export an arrow function?

To export arrow functions in JavaScript, we can use export directly with arrow functions. const hello = () => console. log("hello"); export default hello; to export the hello function as a default export with export default .

What does this line mean export default table?

Export default means you want to export only one value the is present by default in your script so that others script can import that for use. This is very much necessary for code Reusability.

Should I use export or export default?

If a module's primary purpose is to house one specific export, then you should consider exporting it as a default export. This makes both importing and actually using the import a little easier. Further, TypeScript recommends named exports when there are multiple things to export [4].


1 Answers

This is an arrow function (or fat arrow function):

(a, b, c) => { /* ... */ }

Is (almost) equivalent to:

function(a, b, c) { /* ... */ }

The only difference between arrow functions and functions declared with function is that this has lexical binding in arrow functions instead of the confused morass of binding in regular functions.

like image 142
Ethan Brown Avatar answered Oct 11 '22 14:10

Ethan Brown