callback bis
Etudier le code précédent : https://jsfiddle.net/dupont/Lgre6msz/2/
On peut changer le code avec
class BD{
/**
* Finds items based on a query given as a JS object
*
* @param {function} function The function to filtre the query
* @param {function} callback The callback to fire when the query has
* completed running
**/
find ( fxFilter, callback) {
callback(this.villes.filter( fxFilter))
}
On peut changer le code avec
class BD{
/**
* Finds items based on a query given as a JS object
*
* @param {function} function The function to filtre the query
* @param {function} callback The callback to fire when the query has
* completed running
**/
find ( fxFilter, callback) {
callback(this.villes.filter( fxFilter))
}
Callback
class BD{
constructor(villes){
this.villes = villes;
}
/**
* Finds items based on a query given as a JS object
*
* @param {object} query The query to match against (i.e. {ville: 'paris'})
* @param {function} callback The callback to fire when the query has
* completed running
**/
find (query, callback) {
callback(this.villes.filter(function(ville) {
for (let key in query) {
if (query[key] !== ville[key]) {
return false
}
}
return true
}))
}
}
// création d'une base
bd = new BD([{ville:"vincennes",h:2},{ville:"paris",h:2},{ville:"roubais",h:1}]);
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 1`)
bd.find({h:2},function(villes){
for(let ville of villes){
ville["h"]=30;
}
});
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 2`)
bd.find({ville:"paris"},function(villes){
for(let ville of villes){
ville["capitale"]=true;
}
});
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p>`)
https://jsfiddle.net/dupont/Lgre6msz/2/
constructor(villes){
this.villes = villes;
}
/**
* Finds items based on a query given as a JS object
*
* @param {object} query The query to match against (i.e. {ville: 'paris'})
* @param {function} callback The callback to fire when the query has
* completed running
**/
find (query, callback) {
callback(this.villes.filter(function(ville) {
for (let key in query) {
if (query[key] !== ville[key]) {
return false
}
}
return true
}))
}
}
// création d'une base
bd = new BD([{ville:"vincennes",h:2},{ville:"paris",h:2},{ville:"roubais",h:1}]);
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 1`)
bd.find({h:2},function(villes){
for(let ville of villes){
ville["h"]=30;
}
});
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 2`)
bd.find({ville:"paris"},function(villes){
for(let ville of villes){
ville["capitale"]=true;
}
});
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p>`)
https://jsfiddle.net/dupont/Lgre6msz/2/
Map en action
let p = '<h1>escapeForHTML<h1>';
let regex = /[<>&]/gi;
let r = new Map([
['>','>'],
['&','&'],
['<', '<'],
]);
let f = c => r.get(c)
document.body.insertAdjacentHTML("afterbegin",p);
document.body.insertAdjacentHTML("afterbegin",p.replace(regex,f));
let regex = /[<>&]/gi;
let r = new Map([
['>','>'],
['&','&'],
['<', '<'],
]);
let f = c => r.get(c)
document.body.insertAdjacentHTML("afterbegin",p);
document.body.insertAdjacentHTML("afterbegin",p.replace(regex,f));
reduce
const posts = [
{id: "moi", upVotes: 20},
{id: "toi", upVotes: 89},
{id: "lui", upVotes: 1},
{id: "eux", upVotes: 123},];
let [premier,second] = posts.reduce((acc, cur) => [cur, ...acc].sort((a,b) => b.upVotes - a.upVotes).slice(0,2),[]);
console.log(`Au second tour sont présents : ${premier.id} et ${second.id}`);
{id: "moi", upVotes: 20},
{id: "toi", upVotes: 89},
{id: "lui", upVotes: 1},
{id: "eux", upVotes: 123},];
let [premier,second] = posts.reduce((acc, cur) => [cur, ...acc].sort((a,b) => b.upVotes - a.upVotes).slice(0,2),[]);
console.log(`Au second tour sont présents : ${premier.id} et ${second.id}`);
Générateur
class Matrix {
constructor(width, height, element = (x, y) => undefined) {
this.width = width;
this.height = height;
this.content = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
this.content[y * width + x] = element(x, y);
}
}
}
get(x, y) {
return this.content[y * this.width + x];
}
set(x, y, value) {
this.content[y * this.width + x] = value;
}
*[Symbol.iterator]() {
let i=0;
while (i < this.height*this.width) {
yield {
x : i%this.width,
y : Math.floor(i/this.width),
value : this.content[i]
}
i++;
}
}
}
const width = 3;
let matrix = new Matrix(width, width, (x, y) => `[${x},${y}]= ${y * width + x}`);
for (let {value,x,y} of matrix) {
console.log(x,y,value);
}
https://es6console.com/jppky2fw/
0 0 [0,0]= 0
1 0 [1,0]= 1
2 0 [2,0]= 2
0 1 [0,1]= 3
1 1 [1,1]= 4
2 1 [2,1]= 5
0 2 [0,2]= 6
1 2 [1,2]= 7
2 2 [2,2]= 8
Autre exemple : lien
constructor(width, height, element = (x, y) => undefined) {
this.width = width;
this.height = height;
this.content = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
this.content[y * width + x] = element(x, y);
}
}
}
get(x, y) {
return this.content[y * this.width + x];
}
set(x, y, value) {
this.content[y * this.width + x] = value;
}
*[Symbol.iterator]() {
let i=0;
while (i < this.height*this.width) {
yield {
x : i%this.width,
y : Math.floor(i/this.width),
value : this.content[i]
}
i++;
}
}
}
const width = 3;
let matrix = new Matrix(width, width, (x, y) => `[${x},${y}]= ${y * width + x}`);
for (let {value,x,y} of matrix) {
console.log(x,y,value);
}
https://es6console.com/jppky2fw/
0 0 [0,0]= 0
1 0 [1,0]= 1
2 0 [2,0]= 2
0 1 [0,1]= 3
1 1 [1,1]= 4
2 1 [2,1]= 5
0 2 [0,2]= 6
1 2 [1,2]= 7
2 2 [2,2]= 8
Autre exemple : lien
Inscription à :
Articles (Atom)