与导出默认值一起使用时,javascript 中的“=>”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32618193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:31:39  来源:igfitidea点击:

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

javascriptangularjsecmascript-6

提问by KAD

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

我正在分析angular-file-upload插件的一些 angular js 源代码,但在尝试理解某些代码时遇到了一些问题。

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

我知道这export是新 ES6 标准的一部分,它用于从给定文件(或模块)导出函数和对象。

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?

=>这个语句中的运算符有什么用?

回答by Ethan Brown

This is an arrowfunction(or fat arrowfunction):

这是一个箭头函数(或胖箭头函数):

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

Is (almost) equivalent to:

是(几乎)等价于:

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

The only difference between arrow functions and functions declared with functionis that thishas lexical binding in arrow functions instead of the confused morass of binding in regular functions.

箭头函数和用 with 声明的函数之间的唯一区别是在箭头函数functionthis有词法绑定,而不是在常规函数中绑定的混乱泥沼。

回答by Jon Snow

Its an ES6 arrow function. In your case, it accounts to something like:

它是一个ES6 箭头函数。在你的情况下,它说明了以下内容:

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _angular = angular;
var copy = _angular.copy;
var extend = _angular.extend;
var forEach = _angular.forEach;
var isObject = _angular.isObject;
var isNumber = _angular.isNumber;
var isDefined = _angular.isDefined;
var isArray = _angular.isArray;
var element = _angular.element;

exports["default"] = function (fileUploaderOptions, $rootScope, $http, $window, FileLikeObject, FileItem) {
    var File = $window.File;
    var FormData = $window.FormData;

    var FileUploader = function FileUploader() {
        _classCallCheck(this, FileUploader);
    };

    // class implemention....

    return FileUploader;
};

module.exports = exports["default"];

Note that this was compiled by Babel.

请注意,这是由Babel编译的。