Snippets JavaScript
Je rassemble ici quelques snippets JavaScript. Une liste qui sera certainement complétée avec le temps :)
Un élément aléatoire d'un tableau ? Rien de plus simple !
var items = [123, 'John', 11111, 'Doe', 'foo', 2924, 'bar', 'foobar', 55, 'baz'];
var randomItem = items[Math.floor(Math.random() * items.length)];
Un nombre aléatoire entre un minimum et un maximum ? Voilà !
var x = Math.floor(Math.random() * (max - min + 1)) + min;
Une trouvaille sur « SO » : le if/else raccourci en JavaScript. Je ne savais pas que c'était possible. Un langage très puissant… :D
var y = (x == 2 ? "yes" : "no");
Source : stackoverflow.com/questions/11069278
Paramètres optionnels
if (typeof optionalArg === 'undefined') {
optionalArg = 'default';
}
Promise
new Promise(function(resolve, reject) {
alert("Aktion 1");
resolve();
}).then(function() {
alert("Aktion 1 beendet");
});