2016-08-10 45 views
2

目前我正在学习React和Redux。我找到了一个样板文件,并且正在研究所有示例代码。我的问题是我不完全明白这个ES6语法的含义。ES6的未知意义语法

我迄今了解到的情况是hello =() => "Hello";将相当于:

hello = function hello() { 
    return "Hello"; 
}; 

然后改变上述以hello = name => "Hello " + name;将其转换为:

hello = function hello(name) { 
    return "Hello " + name; 
}; 

这一切都非常有意义,基本上它只是缩短它,所以你不必编写函数和它的return语句。然而,我遇到了一些我不能说服我的头脑的语法。这是因为如下:

const mapActionCreators = { 
    increment:() => increment(1), 
    doubleAsync 
} 

上面的代码转换为:

var mapActionCreators = { 
    increment: function (_increment) { 
    function increment() { 
     return _increment.apply(this, arguments); 
    } 

    increment.toString = function() { 
     return _increment.toString(); 
    }; 

    return increment; 
    }(function() { 
    return increment(1); 
    }), 
    doubleAsync: doubleAsync 
}; 

我明白() => increment(1)在这种情况下被翻在地:

(function() { 
    return increment(1); 
}), 

总的来说,我想我的问题是,increment:如何转换为:

increment: function (_increment) { 
    function increment() { 
     return _increment.apply(this, arguments); 
    } 

    increment.toString = function() { 
     return _increment.toString(); 
    }; 

    return increment; 
} 

代码是什么意思?

回答

6

箭头功能从他们在创建范围捕捉this值。

apply让你调用一个函数和明确的this在它的价值。

其余代码只是将正确的this提供给函数。

(和toString正在确保权利函数获得字符串如果您尝试将生成的函数字符串化)。