1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function forevery( list, callback ) {
var i = 0,
n = list.length;
for ( i = 0; i < n; i++ ) {
/*
* Exécute fonction( index ) dans le
* contexte de l'objet list.
* argument : i
*/
callback.call( list, i );
};
}
forevery( [ 'a', 42, false, {} ], function( index ) {
// console, touche F12
console.log( "index = ", index );
console.log( "this = ", this );
console.log( "this[ index ] = ", this[ index ] );
console.log( "this[ index + 1 ] = ", this[ index + 1 ] );
console.log( "this[ index - 1 ] = ", this[ index - 1 ] );
/*
* index = 0
* this = ["a", 42, false, Object]
* this[ index ] = a
* this[ index + 1 ] = 42
* this[ index - 1 ] = undefined
*
* index = 1
* this = ["a", 42, false, Object]
* this[ index ] = 42
* this[ index + 1 ] = false
* this[ index - 1 ] = a
*
* etc.
*/
}); |
Partager