← 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-04-19
1// JS Tip of the Day
2// Displays tabular data as a table
3
4console.table(['Batman', 'Superman', 'Green Arrow']);
5console.table({ name: 'Batman', weapon: 'Batarang', vehicle: 'Batmobile' });
6console.table([
7 { name: 'Batman', superPower: 'Money', partner: 'Robin' },
8 { name: 'Superman', superPower: 'Strenght', partner: 'Wonder Girl' },
9 ]);
2021-04-16
1// JS Tip of the Day
2// How to detect the type of a function
3
4Object.prototype.toString.call(function() {}); // [object Function]
5Object.prototype.toString.call(function*() {}); // [object GeneratorFunction]
6Object.prototype.toString.call(async function() {}); // [object AsyncFunction]
2021-04-15
1// JS Tip of the Day
2// Simple and Quick Array Comparison (A better solution)
3
4const arrayComparison = (firstArray, secondArray) =>
5 firstArray.length === secondArray.length &&
6 firstArray.every((v, i) => v === secondArray[i]);
7
8const heroes = ['Batman', 'Superman', 'Green Arrow'];
9const moreHeroes = [new String('Batman'), 'Superman', 'Green Arrow'];
10
11arrayComparison(heroes, moreHeroes); // false
12
13moreHeroes[0] = 'Batman';
14
15arrayComparison(heroes, moreHeroes); // true
2021-04-14
1// JS Tip of the Day
2// Simple and Quick Array Comparison
3
4const arrayComparison = (firstArray, secondArray) => {
5 return JSON.stringify(firstArray) === JSON.stringify(secondArray);
6}
7
8const heroes = ['Batman', 'Superman', 'Green Arrow'];
9const moreHeroes = ['Batman', 'Superman', 'Green Arrow'];
10
11arrayComparison(heroes, moreHeroes); // true
12/*
13 Attention!
14 It falls with different values serialized but
15 with the same string conversion like null, undefined, ...
16*/
2021-04-13
1// JS Tip of the Day
2// Set an Object's non-enumerable property
3
4const hero = { name: 'Batman', weapon: 'Batarang', partner: 'Robin' };
5
6for (let [key, value] of Object.entries(hero)) {
7 console.log(key, value);
8 // ['name', 'Batman'], ['weapon', 'Batarang'], ['partner', 'Robin']
9}
10
11// Set enumerable data descriptor of an Object's property to false
12Object.defineProperty(hero, 'partner', { enumerable: false });
13
14for (let [key, value] of Object.entries(hero)) {
15 console.log(key, value);
16 // ['name', 'Batman'], ['weapon', 'Batarang']
17}
18
19// Get only enumerable properties with common methods or loops
20console.log(Object.keys(hero));
21// Get all properties (without Symbol properties)
22console.log(Object.getOwnPropertyNames(hero));
23
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.