1. Arrow functions provide a concise syntax for defining anonymous functions and avoiding issues with binding 'this'.
2. They are best used for list processing and anonymous callback functions.
3. Arrow functions lexically bind 'this' from the enclosing context and cannot be used as constructors or contain yield expressions.
4. When a function requires a constructor, generator, changing 'this' binding, or needs arguments - a regular function expression is better.
9. Lexical(vs(Dynamic(this
function hasBeenBought(company, cb){ cb(true); }
function Company() { this.name = 'Concur'; }
Company.prototype.newName = function() {
console.log(this.name);
hasBeenBought(this.name, function(bool) {
if (bool) console.log(this.name + ', an SAP Company');
});
}
let concur = new Company();
concur.newName();
10. Lexical(vs(Dynamic(this
function hasBeenBought(company, cb){ cb(true); }
function Company() { this.name = 'Concur'; }
Company.prototype.newName = function() {
console.log(this.name); // "Concur"
hasBeenBought(this.name, function(bool) {
if (bool) console.log(this.name + ', an SAP Company');
// ", an SAP Company"
});
}
let concur = new Company();
concur.newName();
11. Lexical(vs(Dynamic(this(with(=>
function hasBeenBought(company, cb){ cb(true); }
function Company() { this.name = 'Concur'; }
Company.prototype.newName = function() {
console.log(this.name);
hasBeenBought(this.name, bool => {
if (bool) console.log(this.name + ', an SAP Company');
});
}
let concur = new Company();
concur.newName();
12. Lexical(vs(Dynamic(this(with(=>
function hasBeenBought(company, cb){ cb(true); }
function Company() { this.name = 'Concur'; }
Company.prototype.newName = function() {
console.log(this.name); // "Concur"
hasBeenBought(this.name, bool => {
if (bool) console.log(this.name + ', an SAP Company');
// "Concur, an SAP Company"
});
}
let concur = new Company();
concur.newName();
13. Lexical(Arguments
function f() {
var args = arguments;
var g = () => arguments;
assert.equal(args, g())
}
f()
arrow%func*ons%reference%the%arguments%from%the%parent
14. Yield&with&func.on
function* incr() {
var index = 0;
while (true) {
yield index++;
}
}
let forever = incr();
forever.next() // 0
forever.next() // 1
forever.next() // 2
15. Yield&with&arrow
noIncr = () => {
var index = 0;
while (true) {
yield index++;
}
}
// SyntaxError: arrow function may not contain yield