← 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-03-26
1// JS Tip of the Day
2// Count the number of times the console method is called
3
4const getHero = (name) => name;
5const hero = { name: 'Batman' };
6
7// Counter with a Label
8console.count(getHero('Batman')); // Batman: 1
9console.count(getHero('Batman')); // Batman: 2
10console.count(getHero('Batman')); // Batman: 3
11console.count(hero.name); // Batman: 4
12
13console.count(getHero('Superman')); // Superman: 1
14console.count(getHero('Superman')); // Superman: 2
15console.count('Superman'); // Superman: 3
16
17// Reset counter
18console.countReset('Superman');
19
20console.count('Superman'); // Superman: 1
21
22// Counter without a Label
23console.count(); // default: 1
24console.count(); // default: 2
2021-03-25
1// JS Tip of the Day
2// Convert Object to Primitives
3
4const hero = {
5 name: 'Batman',
6 [Symbol.toPrimitive](hint) {
7 console.log(`hint is ${hint}`);
8 if (hint == 'number') {
9 return Infinity;
10 }
11 if (hint == 'string') {
12 return `I'm ${this.name}`;
13 }
14 return true;
15 }
16};
17
18// Convert to number...or Bruce Wayne's Bank balance! :D
19console.log(+hero); // "hint is number" - Infinity
20
21// Convert to string
22console.log(`${hero}`); // "hint is string" - "I'm Batman"
23
24// Convert to default
25console.log(hero + ''); // "hint is default" - true
2021-03-24
1// JS Tip of the Day
2// How to force to execute a function like a constructor
3
4function Hero(name) {
5 // new.target is true if called by the new operator
6 if (!new.target) {
7 return new Hero(name);
8 }
9
10 this.name = name;
11}
12
13let bestHero = Hero('Batman');
2021-03-23
1// JS Tip of the Day
2// Exit nested loops (but avoid nesting loop!)
3
4outerLoop:
5for(let outerIndex = 0; outerIndex < 10; outerIndex++) {
6 innerLoop:
7 for(innerIndex = 0; innerIndex < 10; innerIndex++) {
8 if(outerIndex === 2 && innerIndex === 5) {
9 break outerLoop;
10 }
11 console.log(outerIndex, innerIndex);
12 }
13}
2021-03-22
1// JS Tip of the Day
2// One-line Arrow Function with undefined returns only
3
4const getHero = () => 'Batman';
5const getUndefinedHero = () => void 'Batman';
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.