As arrow functions a princĂpio podem parecer confusas (experiĂŞncia prĂłpria (◕︵◕)), mas depois de um tempo, Ă© possĂvel entender sua sintaxe mais curta e a mágica do escopo do this
.
Vários parâmetros
Sem arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var oldWay = function(name, nickname) { | |
return 'My name is ' + nickname + ', ' + name; | |
}; | |
console.log( oldWay('James Bond', 'Bond') ); | |
// My name is Bond, James Bond |
Com arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let newWay = (name, nickname) => { | |
return 'My name is ' + nickname + ', ' + name; | |
}; | |
console.log( newWay('James Bond', 'Bond') ); | |
// My name is Bond, James Bond |
Ou de uma maneira um pouco mais curta.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let newWay2 = (name, nickname) => 'My name is ' + nickname + ', ' + name; | |
console.log( newWay2('James Bond', 'Bond') ); | |
// My name is Bond, James Bond |
Apenas um parâmetro
Sem arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var one = function(what) { | |
return 'I ' + what + ' you'; | |
}; | |
console.log( one('hate') ); | |
// I hate you |
Com arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var oneNew = what => 'I ' + what + ' you'; | |
console.log( oneNew('hate') ); | |
// I hate you |
Escopo
Sem arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sandwich = { | |
bread: 'integral', | |
cheese: 'white', | |
prepare: function() { | |
return 'I want a sandwich with ' + this.bread + ' bread and ' + this.cheese + ' cheese'; | |
}, | |
make: function() { | |
var that = this; // (◕︵◕) | |
window.setTimeout( function () { | |
console.log( that.prepare() ); | |
}, 100 ); | |
} | |
}; | |
// sandwich.make(); | |
// I want a sandwich with integral bread and white cheese |
Com arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let newSandwich = { | |
bread: 'integral', | |
cheese: 'white', | |
prepare: function() { | |
return 'I want a sandwich with ' + this.bread + ' bread and ' + this.cheese + ' cheese'; | |
}, | |
make: function() { | |
window.setTimeout( () => console.log(this.prepare()), 100 ); | |
} | |
}; | |
// newSandwich.make(); | |
// I want a sandwich with integral bread and white cheese |
Exemplo com map
Sem arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sample = [1, 2, 3, 4, 5]; | |
var double = sample.map(function(item) { | |
return item * 2; | |
}); | |
// console.log(double); | |
// [2, 4, 6, 8, 10] |
Com arrow functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sample = [1, 2, 3, 4, 5]; | |
let newDouble = sample.map(item => item * 2); | |
// console.log(newDouble); | |
// [2, 4, 6, 8, 10] |
Aqui vocĂŞ encontra um JS Bin com os exemplos acima.