This file contains hidden or 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
| () => { ... } // brak parametr贸w | |
| x => { ... } // jeden parametr, identyfikator | |
| (x, y) => { ... } // kilka parametr贸w |
This file contains hidden or 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
| // Funkcja z pojedynczym identyfikatorem, opuszczamy nawiasy otaczaj膮ce parametr. | |
| [1, 2, 3].map(x => 2 * x); // [2, 4, 6] | |
| // Funkcja wykorzystuj膮ca destrukturyzacje, pami臋tamy o u偶yciu nawias贸w. | |
| [[1,2], [3,4]].map(([a, b] => a + b); // [3, 7] | |
| // Funkcja z parametrem o warto艣ci domy艣lnej, nawiasy do boju. | |
| const sayHello = (name="World") => console.log(`Hello ${name}`); | |
| sayHello(); // 'Hello World' |
This file contains hidden or 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
| // OK, brak bloku pozwala na wykorzystanie niejawnego return. | |
| const sum = (a, b) => a + b; | |
| // :(, ka偶de wywo艂anie zwr贸ci undefined, wewn膮trz blok贸w wymagane jest jawne return. | |
| const sum = (a, b) => { a + b } | |
| // OK, jawne return. | |
| const sum = (a, b) => { return a + b }; |
This file contains hidden or 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
| const array = [1, 2, 3, 4, 5, 6]; | |
| // Rozwi膮zanie z u偶yciem deklaracji funkcji. | |
| function isEven(num) { | |
| return num % 2 === 0; | |
| } | |
| function square(num) { | |
| return num * num; | |
| } |
This file contains hidden or 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
| // Deklaracja przodem. | |
| function makeAdder(x) { | |
| return function(y) { | |
| return x + y; | |
| } | |
| } | |
| // Do celu =>! | |
| const makeAdder = x => y => x + y; |
This file contains hidden or 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
| const sum = (a, b) // Syntax error. | |
| => { return a + b; }; | |
| const sum = (a, b) // Syntax error. | |
| => a + b; | |
| const sum = (a, b) => // OK | |
| { | |
| return a + b; | |
| }; |
This file contains hidden or 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
| asyncFunc.catch(x => { throw x }); |
This file contains hidden or 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
| // NOT OK, wywo艂anie zwr贸ci undefined | |
| const baz = () => { foo: 'bar' }; | |
| // OK | |
| const baz = () => ({ foo: 'bar' }); |
This file contains hidden or 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
| function Adder(value) { | |
| this.value = value; | |
| } | |
| Adder.prototype.addToArray = function (arr) { | |
| 'use strict'; | |
| return arr.map(function (x) { | |
| return x + this.value; // TypeError. | |
| }); | |
| } |
This file contains hidden or 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
| // Wi膮偶emy this z zakresu addToArray do funkcji mapuj膮cej za pomoc膮 bind(). | |
| Adder.prototype.addToArray = function (arr) { | |
| return arr.map(function (x) { | |
| return x + this.value; | |
| }.bind(this)); | |
| } | |
| // Korzystamy ze zmiennej pomocniczej self, w kt贸rej zapisujemy warto艣膰 wska藕nika this. | |
| Adder.prototype.addToArray = function (arr) { | |
| let self = this; |
OlderNewer