← 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.

75

Using Array Methods in DOM Collections

2021-05-07

1// JS Tip of the Day
2// Using Array Methods in DOM Collections
3
4const nodes = Array.from(document.body.childNodes);
5const textNodes = nodes.filter(node => node.nodeType === 3);
74

How to avoid to adding a new object property with Proxy

2021-05-06

1// JS Tip of the Day
2// How to avoid to adding a new object property with Proxy
3
4let handler = {
5  set: (target, key, value) => {
6      if(!target.hasOwnProperty(key)) {
7        throw new Error('You cannot add a new property!');
8      }
9      target[key] = value;
10      return true;
11  }
12}
13
14const hero = { name: 'Batman' };
15
16const heroProxy = new Proxy(hero, handler);
17
18console.log(heroProxy); // { name: 'Batman' }
19heroProxy.name = 'Superman'; 
20console.log(heroProxy); // { name: 'Batman' }
21heroProxy.partner = 'Robin'; // Error: You cannot add a new property!
73

How to detect localStorage updates between browser tabs or window

2021-05-05

1// JS Tip of the Day
2// How to detect localStorage updates between browser tabs or window
3
4// Browser Tab or Window - http://example.com
5window.addEventListener('storage', (event) => { // or you may use window.onstorage
6  console.log(event.key, event.newValue, event.oldValue, event.url);
7  // Hero, Batman, null, http://example.com/heroes
8});
9
10
11// Another Browser Tab or Window - http://example.com/heroes
12localStorage.setItem('hero', 'Batman');
72

Convert Binary string to text (with a secret and awesome message!)

2021-05-04

1// JS Tip of the Day
2// Convert Binary string to text (with a secret and awesome message!)
3
4const awesomeBinaryMessage = "01001001 00100111 01101101 00100000 01101110 01101111 01110111 00100000 01101111 01100110 01100110 01101001 01100011 01101001 01100001 01101100 01101100 01111001 00100000 01100001 00100000 01000111 01101111 01101111 01100111 01101100 01100101 00100000 01000100 01100101 01110110 01100101 01101100 01101111 01110000 01100101 01110010 00100000 01000101 01111000 01110000 01100101 01110010 01110100 00100001";
5
6const awesomeMessage = awesomeBinaryMessage.split(' ')
7   .map(bin => {
8     return String.fromCharCode(parseInt(bin, 2)) // Binary char to Text char
9   }) 
10   .join('');
11
12// πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰ You have to execute this snippet to read the message! πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰
13console.log(awesomeMessage); 
71

Two types of Interval

2021-05-03

1// JS Tip of the Day
2// Two types of Interval
3
4// No consideration on the end of the execution
5const interval = setInterval(() => {
6 // ...
7}, 1000);
8
9// Sure that the execution has finished
10const myInterval = () => {
11  // ...
12  setTimeout(myInterval, 1000)
13}
14setTimeout(myInterval, 1000);

Contattaci.

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.

Questo sito Γ¨ protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.