以下对函数的三种定义方式是一个意思:
const hello1 = function (firstname) { return `Hello ${firstname}`; }; const hello2 = (firstname) => { return `Hello ${firstname}`; }; const hello3 = firstname => `Hello ${firstname}`; // (°~°)
下面我们用数组的.map()方法的callback调用,来体会以上三种定义函数的方式。
const test = [1, 2, 3]; function doubler (x) { return x * 2; } test.map(doubler); // [2, 4, 6] test.map(function (x) { return x * 2; }); // [2, 4, 6] test.map((x) => { return x * 2; }); // [2, 4, 6] test.map(x => x * 2); // [2, 4, 6] Bingo!
版权声明:本文为原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
关注微信公众号:"cq_xifan";