Javascript ES6语法的未知含义
目前我正在学习React和Redux。我已经找到了一个样板文件,我正在研究所有的示例代码。我的问题是我不完全理解很多ES6语法的含义 到目前为止我学到的是Javascript ES6语法的未知含义,javascript,babeljs,Javascript,Babeljs,目前我正在学习React和Redux。我已经找到了一个样板文件,我正在研究所有的示例代码。我的问题是我不完全理解很多ES6语法的含义 到目前为止我学到的是hello=()=>“hello”相当于: hello = function hello() { return "Hello"; }; 然后将上述内容更改为hello=name=>“hello”+name将其转换为: hello = function hello(name) { return "Hello " + name; };
hello=()=>“hello”代码>相当于:
hello = function hello() {
return "Hello";
};
然后将上述内容更改为hello=name=>“hello”+name代码>将其转换为:
hello = function hello(name) {
return "Hello " + name;
};
这一切都很有道理,基本上只是将其缩短,这样就不必编写函数及其返回语句。然而,我遇到了一些语法,我不能左右我的头。详情如下:
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
};
我知道在这种情况下,()=>增量(1)
将交给:
(function () {
return increment(1);
}),
总的来说,我想我的问题是,如何将增量:
转换为:
increment: function (_increment) {
function increment() {
return _increment.apply(this, arguments);
}
increment.toString = function () {
return _increment.toString();
};
return increment;
}
代码的含义是什么?箭头函数从创建它们的作用域中捕获该的值
apply
允许您调用函数并在其中显式指定this
的值
代码的其余部分只是将正确的this
输入函数
(并且toString
确保在您尝试字符串化生成的函数时,正确的函数得到字符串化)