- Hands-On Machine Learning with JavaScript
- Burak Kanber
- 424字
- 2021-06-25 21:38:19
Arrow functions
One quirky, useful, but somewhat annoying aspect of ES5 JavaScript is its heavy use of callbacks that run asynchronously. You are probably intimately familiar with jQuery code that looks something like this:
$(“#link”).click(function() {
var $self = $(this);
doSomethingAsync(1000, function(resp) {
$self.addClass(“wasFaded”);
var processedItems = resp.map(function(item) {
return processItem(item);
});
return shipItems(processedItems);
});
});
We're forced to create a variable called $self because the original this context is lost in our inner anonymous function. We also have a lot of boilerplate and difficult-to-read code due to needing to create three separate anonymous functions.
Arrow function syntax is both syntactic sugar that helps us write anonymous functions with a shorter syntax, and also a functional update that preserves the context of this inside an arrow function.
For instance, the preceding code may be written in ES6 as follows:
$(“#link”).click(function() {
dozsSomethingAsync(1000, resp => {
$(this).addClass(“wasFaded”);
const processedItems = resp.map(item => processItem(Item));
return shipItems(processedItems);
});
});
You can see in the preceding code that we no longer need a $self variable to preserve this, and our call to .map is much simpler, no longer requiring the function keyword, parentheses, curly braces, or a return statement.
Now let's look at some equivalent functions. Let's look at the following code:
const double = function(number) {
return number * 2;
}
The preceding code would be similar to:
const double = number => number * 2;
// Is equal to:
const double = (number) => { return number * 2; }
In the aforementioned examples, we can omit the parentheses around the number parameter because the function only requires one parameter. If the function required two parameters, we would be required to add parentheses as in the next example. Additionally, if the body of our function only requires one line, we can omit the function body curly braces and omit the return statement.
Let's look at another equivalence, with multiple parameters, as shown in the following code:
const sorted = names.sort(function (a, b) {
return a.localeCompare(b);
});
The preceding code would be similar to:
const sorted = names.sort((a, b) => a.localeCompare(b));
I find that arrow functions make themselves most useful in situations like the preceding one, when you're doing data transformations, especially where using Array.map, Array.filter, Array.reduce, and Array.sort calls with straightforward function bodies. Arrow functions are less useful in jQuery because of jQuery's tendency to give you data using the this context, which you don't receive with anonymous arrow functions.
- Design for the Future
- 基于LabWindows/CVI的虛擬儀器設(shè)計與應(yīng)用
- MicroPython Projects
- Photoshop CS3特效處理融會貫通
- Data Wrangling with Python
- 80x86/Pentium微型計算機原理及應(yīng)用
- VB語言程序設(shè)計
- 網(wǎng)絡(luò)組建與互聯(lián)
- 完全掌握AutoCAD 2008中文版:機械篇
- 網(wǎng)絡(luò)安全技術(shù)及應(yīng)用
- 從零開始學(xué)JavaScript
- 大數(shù)據(jù)導(dǎo)論
- Natural Language Processing and Computational Linguistics
- 手把手教你學(xué)Photoshop CS3
- Eclipse RCP應(yīng)用系統(tǒng)開發(fā)方法與實戰(zhàn)