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

101

Singleton Pattern Class (without ES Module)

2021-06-21

1// JS Tip of the Day
2// Singleton Pattern Class (without ES Module)
3
4class Singleton {
5  constructor() {
6    if (!Singleton.instance) {
7      console.log('Create singleton instance');
8      Singleton.instance = this;
9    }
10    console.log('Get singleton instance');
11    return Singleton.instance;
12  }
13}
14
15const singletonA = new Singleton(); 
16// Create singleton instance
17// Get singleton instance
18const singletonB = new Singleton(); 
19// Get singleton instance
20
21console.log(singletonA === singletonB); // true
100

WTFJS (old but gold)

2021-06-13

1// JS Tip of the Day - This is my 100th tip!
2// WTFJS (old but gold) 
3
4//You have to watch it! :D
5window.location.assign('https://www.youtube.com/watch?v=et8xNAc2ic8'); 
6
7//You have to visit it! :D
8window.location.assign('https://github.com/denysdovhan/wtfjs'); 
99

Simple Interaction with HID (Experimental)

2021-06-12

1// JS Tip of the Day
2// Simple Interaction with HID (Experimental)
3
4try {
5	const devices = await navigator.hid.requestDevice({filters: []});
6    console.log(`Device: ${devices[0].productName}`);
7    devices[0].open();
8    devices[0].addEventListener('inputreport', e => console.log(e));
9}
10catch(e) {
11	console.log(`No Device: ${e}`);
12}
98

How to request and detect the USB Devices (Experimental)

2021-06-10

1// JS Tip of the Day
2// How to request and detect the USB Devices (Experimental)
3
4// Request a Device
5const filters = {filters: []}; // You could filter by vendorId/productId
6
7await navigator.usb.requestDevice(filters)
8.then(device => console.log(`Device: ${device.productName}`))
9.catch(e => console.log(`No Device: ${e}`));
10
11// Get all a paired attached devices
12await navigator.usb.getDevices()
13.then(devices => {
14  console.log(`Devices: ${devices.length}`);
15  devices.forEach(device => console.log(`Device: ${device.productName}`));
16});
17
18// NB: Yes...I used top level await! :D
97

Get selected text on a web page

2021-06-09

1// JS Tip of the Day
2// Get selected text on a web page
3
4const getSelectedText = () => window.getSelection().toString();
5console.log(getSelectedText()); // 'Selected Text!'

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.