Devenet Devenet

Liens en vrac

Quelques 337 marque-pages glanés pendant ma navigation sur le Web.

11 résultats pour #javascript×

RSS ATOM

An alternative to if/else and switch in JavaScript

http://blog.wax-o.com/2015/05/an-alternative-to-if-else-and-switch-in-javascript/

Comment éviter un switch ou un if/else en JavaScript : avec un tableau.

let values = {
   a: 1,
   b: 2,
};
let foo = values[ bar ] || 3;

est équivalent à

let foo = '';
if ( bar === 'a' )
   foo = 1;
else if ( bar === 'b' )
   foo = 2;
else
   foo = 3;

Detect down/up scrolling in Javascript

http://lehollandaisvolant.net/?mode=links

// Initial state
var scrollPos = 0;
// adding scroll event
window.addEventListener('scroll', function(){ scrolling() });

// the function : compares the new scrolling state with the previous
// (this allows detecting either “up” or “down” scrolling)
// then saves the new in the $previous for the next iteration.

function scrolling() {
if ((document.body.getBoundingClientRect()).top > scrollPos) {
console.log('scrolling DOWN');
} else {
console.log('scrolling UP');
}
scrollPos = (document.body.getBoundingClientRect()).top;
}

7 lines JavaScript library for calling asynchronous functions

http://krasimirtsonev.com/blog/article/7-lines-JavaScript-library-for-calling-asynchronous-functions

Il y a aussi http://stackoverflow.com/questions/899102/how-do-i-store-javascript-functions-in-a-queue-for-them-to-be-executed-eventuall

// Function wrapping code.
// fn - reference to function.
// context - what you want "this" to be.
// params - array of parameters to pass to function.
var wrapFunction = function(fn, context, params) {
   return function() {
       fn.apply(context, params);
   };
}

// Create my function to be wrapped
var sayStuff = function(str) {
   alert(str);
}

// Wrap the function.  Make sure that the params are an array.
var fun1 = wrapFunction(sayStuff, this, ["Hello, world!"]);
var fun2 = wrapFunction(sayStuff, this, ["Goodbye, cruel world!"]);

// Create an array and append your functions to them
var funqueue = [];
funqueue.push(fun1);
funqueue.push(fun2);

// Remove and execute all items in the array
while (funqueue.length > 0) {
   (funqueue.shift())();  
}