原文 https://thewebjuice.com/es6-arrows/
1 使用es6箭头定义匿名函数
(msg)=>console.log('Hello World')
es5
'use strict'; (function (msg) {
return console.log('Hello World');
});
2 单个参数和多个参数
// Multiple Parameter
(arg1,arg2,arg3,arg4)=>{
return arg1+arg2+arg3+arg4
} // Single Parameter
(arg1)=>{
return arg1
}
es5
"use strict"; // Multiple Parameter
(function (arg1, arg2, arg3, arg4) {
return arg1 + arg2 + arg3 + arg4;
}); // Single Parameter
(function (arg1) {
return arg1;
});
3定义闭包
// Single Line Closure
var SayHello=(hello)=>console.log(hello) // Multi Line Closure
var SayHelloAgain=(hello)=>{
console.log('This is a multiline Closure')
console.log(hello)
} // Calling the Two above Closure
SayHello('Hey I am ES6 Arrow')
SayHelloAgain('Heya Again!!!');
es5
'use strict'; // Single Line Closure
var SayHello = function SayHello(hello) {
return console.log(hello);
}; // Multi Line Closure
var SayHelloAgain = function SayHelloAgain(hello) {
console.log('This is a multiline Closure');
console.log(hello);
}; // Calling the Two above Closure
SayHello('Hey I am ES6 Arrow');
SayHelloAgain('Heya Again!!!');
4 Literal Syntax
var createObject = (x,y,color)=>({x:x,y:y,z:z})
es5
"use strict"; var createObject = function createObject(x, y, color) {
return { x: x, y: y, z: z };
};