Variation | Example |
---|---|
No parameters | () => console.log('Hi') or _ => console.log('Hi') |
Single parameter | x => x * x |
Multiple parameters | (x, y) => x * y |
Body block | x => { let y = x * 3; return y; } |
Returns Object literals | x => ({y: x}) |
Object methods
var obj = {
a: 1,
get: () => {
return this.a;
}
};
console.log(obj.get()); //undefined
Callback functions with dynamic context
var button = document.getElementById('press');
button.addEventListener('click', () => {
this.classList.toggle('on'); //TypeError
});