← Torna al blog
Dev Tips
Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.
Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.
2021-06-28
1// JS Tip of the Day
2// Get the current position of the device with Geolocation API
3
4const options = { enableHighAccuracy: true, timeout: 2000, maximumAge: 0 };
5
6function success({ coords }) {
7 console.log(`
8 Lat: ${coords.latitude}
9 Long: ${coords.longitude}
10 Accuracy (meters): ${coords.accuracy}`
11 );
12}
13
14function error(err) {
15 console.warn(`Error: ${err.code} - ${err.message}`);
16}
17
18if (window.navigator && window.navigator.geolocation) {
19 navigator.geolocation.getCurrentPosition(success, error, options);
20}
2021-06-25
1// JS Tip of the Day
2// Promise.allSettled method
3
4const heroPromises = Promise.allSettled([
5 Promise.resolve('Batman'),
6 Promise.resolve('Superman'),
7 Promise.reject('Spiderman is not a DC Hero!')
8]);
9
10heroPromises.then(promises => {
11 promises.forEach(item => console.log(item));
12});
13
14// {status: "fulfilled", value: "Batman"}
15// {status: "fulfilled", value: "Superman"}
16// {status: "rejected", reason: "Spiderman is not a DC Hero!"}
2021-06-24
1// JS Tip of the Day
2// Promise.all method
3
4const heroPromises = Promise.allSettled([
5 Promise.resolve('Batman'),
6 Promise.resolve('Superman')
7]);
8
9const anotherHeroPromises = Promise.allSettled([
10 Promise.resolve('Batman'),
11 Promise.reject('Spiderman is not a DC Hero!')
12])
13
14setTimeout(function() {
15 console.log(heroPromises);
16 // Promise { <state>: 'fullfilled', <value>: Array[2] }
17
18 console.log(anotherHeroPromises);
19 // Promise { <state>: 'rejected', <reason>: 'Spiderman is not a DC Hero!' }
20});
2021-06-23
1// JS Tip of the Day
2// Create an object with the number of occurrences of the array elements
3
4const occurrences = arr => {
5 return arr.reduce((obj, item) => {
6 obj[item] = obj[item] ? obj[item] + 1 : 1;
7 return obj;
8 }, {});
9}
10
11occurrences(['Batman', 'Superman', 'Batman', 'Cyborg']);
12// { Batman: 2, Superman: 1, Cyborg: 1 }
13
2021-06-22
1// JS Tip of the Day
2// Using Function.length
3
4function myFunctionWithoutParams() {}
5console.log(myFunctionWithoutParams.length); // 0
6
7function myFunctionWithOneParam(firstParam) {}
8console.log(myFunctionWithOneParam.length); // 1
9
10function myFunctionWithTwoParams(firstParam, secondParam) {}
11console.log(myFunctionWithTwoParams.length); // 2
12
13// rest parameter is not counted
14function myFunctionWithRest(firstParam, secondParam, ...rest) {}
15console.log(myFunctionWithRest.length); // 2
16
17// only parameters before the first one with a default value is counted
18function myFunctionWithDefault(firstParam, secondParam = '', thirdParam) {}
19console.log(myFunctionWithDefault.length); // 1
Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le competenze del tuo team in ambito di programmazione e sviluppo?
Oppure vuoi semplicemente prendere prendere un caffè con noi e vedere la nostra collezione di Action Figure, allora scrivici tramite questo form.
Se, invece, vuoi far parte del team, guarda le nostre offerte di lavoro.