Tip of the day - JavaScript

Un trucchetto al giorno per mantenerti allenato o per conoscere qualcosa di nuovo sul magico mondo di JavaScript.

103

Create an object with the number of occurrences of the array elements

2021-06-23 - JavaScript

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
#JStipoftheday
102

Using Function.length

2021-06-22 - JavaScript

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 
#JStipoftheday
101

Singleton Pattern Class (without ES Module)

2021-06-21 - JavaScript

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
#JStipoftheday
100

WTFJS (old but gold)

2021-06-13 - JavaScript

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'); 
#JStipoftheday
99

Simple Interaction with HID (Experimental)

2021-06-12 - JavaScript

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}
#JStipoftheday

Preferisci ricevere i tips via mail? Iscriviti alla newsletter.

Iscriviti alla newsletter

Tip of the day - JavaScript

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le tue competenze o quelle del tuo team in ambito di programmazione e sviluppo?

Anche se - semplicemente - vuoi prendere un caffè con noi o vedere la nostra collezione di Action Figures scrivici tramite questo form.

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