Mic*_*nes 2 javascript babeljs
目前我正在研究React和Redux.我找到了一个样板文件,我正在查看所有示例代码.我的问题是我不完全理解这个ES6语法的含义.
到目前为止我学到的东西hello = () => "Hello";相当于:
hello = function hello() {
return "Hello";
};
Run Code Online (Sandbox Code Playgroud)
然后更改上面的内容hello = name => "Hello " + name;将其转换为:
hello = function hello(name) {
return "Hello " + name;
};
Run Code Online (Sandbox Code Playgroud)
这一切都很有意义,基本上它只是缩短它,所以你不必编写函数及其return语句.然而,我遇到了一些我不能说清楚的语法.它如下:
const mapActionCreators = {
increment: () => increment(1),
doubleAsync
}
Run Code Online (Sandbox Code Playgroud)
上面的代码转换为:
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
};
Run Code Online (Sandbox Code Playgroud)
据我所知,() => increment(1)在这种情况下,请转入:
(function () {
return increment(1);
}),
Run Code Online (Sandbox Code Playgroud)
总的来说,我想我的问题是,如何increment:转换为:
increment: function (_increment) {
function increment() {
return _increment.apply(this, arguments);
}
increment.toString = function () {
return _increment.toString();
};
return increment;
}
Run Code Online (Sandbox Code Playgroud)
代码是什么意思?
箭头函数捕获this创建它们的范围的值.
apply允许您调用函数并显式调用其中的值this.
其余代码只是this为函数提供正确的代码.
(并且如果您尝试对生成的函数进行字符串化,则toString确保正确的函数被字符串化).
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |