javascript - sets & weaksets

Transcription

javascript - sets & weaksets
S e t & & We a k S e t
J AVA S C R I P T (Programmation Internet) V O L . X X I I
Pour Débutants
J.B. Dadet DIASOLUKA Luyalu Nzoyifuanga
+243 - 851278216 - 899508675 - 995624714 - 902263541 - 813572818
[email protected]
LES « SET » && « WEAKSET » :
C’est une forme d’Array d’itérables dans laquelle chaque élément ne peut
figurer qu’une seule fois, mais dont la syntaxe est similaire à celle des
« Typed Arrays ».
<script type="text/javascript"> "use strict";
let t;
const a=[14, {a:12, b:17}, 19, 14, "Inco", [14, 15], 10,
14, 15];
console.log(a);
// Array(9) [ 14, {…}, 19, 14, "Inco", (2) […], 10, 14, 15
]
// (9) […]
//
0: 14
//
1: Object { a: 12, b: 17 }
//
1: {…}
//
a: 12
//
b: 17
//
<prototype>: {…}
//
2: 19
//
3: 14
//
4: "Inco"
//
5: Array [ 14, 15 ]
//
5: (2) […]
//
0: 14
//
1: 15
//
length: 2
//
<prototype>: [
//
6: 10
//
7: 14
//
8: 15
//
length: 9
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
//
<prototype>: []
const st = new Set(a);
console.log(st);
// Set(7) [ 14, {…}, 19, "Inco", (2) […], 10, 15 ]
// Set(7)
//
size: 7
//
<entries>
//
0: 14
//
1: Object { … }
//
1: {…}
//
a: 12
//
b: 17
//
<prototype>: Object { … }
//
2: 19
//
3: "Inco"
//
4: Array [ … ]
//
4: (2) […]
//
0: 14
//
1: 15
//
length: 2
//
<prototype>: Array []
//
5: 10
//
6: 15
//
<prototype>: Object { … }
//
<prototype>: {…}
//
add: function add()
//
clear: function clear()
//
constructor: function Set()
//
delete: function delete()
//
entries: function entries()
//
forEach: function forEach()
//
has: function has()
//
keys: function values()
//
size: Getter
//
values: function values()
//
Symbol(Symbol.iterator): function values()
//
Symbol(Symbol.toStringTag): "Set"
//
<prototype>: {…}
const fl = new Float64Array(a);
console.log(fl);
// Float64Array(9) [ 14, NaN, 19, 14, NaN, NaN, 10, 14, 15
Set && WeakSet
- 2/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
]
// Float64Array(9)
//
0: 14
//
1: NaN
//
2: 19
//
3: 14
//
4: NaN
//
5: NaN
//
6: 10
//
7: 14
//
8: 15
//
buffer: ArrayBuffer { byteLength: 72 }
//
buffer: ArrayBuffer
//
byteLength: 72
//
<prototype>: ArrayBufferPrototype { … }
//
<prototype>: ArrayBufferPrototype
//
byteLength: Getter
//
constructor: function ArrayBuffer()
//
slice: function slice()
//
Symbol(Symbol.toStringTag): "ArrayBuffer"
//
<prototype>: {…
//
byteLength: 72
//
byteOffset: 0
//
length: 9
//
<prototype>: Float64ArrayPrototype { … }
//
<prototype>: Float64ArrayPrototype
//
BYTES_PER_ELEMENT: 8
//
constructor: function Float64Array()
//
constructor: Float64Array()
//
BYTES_PER_ELEMENT: 8
//
length: 3
//
name: "Float64Array"
//
prototype: Float64ArrayPrototype { … }
//
<prototype>: TypedArray()
//
from: function from()
//
length: 0
//
name: "TypedArray"
//
of: function of()
//
prototype: TypedArrayPrototype { … }
//
Symbol(Symbol.species): Getter
//
<prototype>: ()
//
apply: function apply()
//
arguments: null
Set && WeakSet
- 3/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
JavaScript TomeXXII
bind: function bind()
call: function call()
caller: null
constructor: function Function()
length: 0
name: ""
toSource: function toSource()
toString: function toString()
Symbol(Symbol.hasInstance):
function Symbol.hasInstance()
<prototype>: {…}
<prototype>: TypedArrayPrototype { … }
<prototype>: TypedArrayPrototype
buffer: Getter
byteLength: Getter
byteOffset: Getter
constructor: function TypedArray()
copyWithin: function copyWithin()
entries: function entries()
every: function every()
fill: function fill()
filter: function filter()
find: function find()
findIndex: function findIndex()
forEach: function forEach()
includes: function includes()
indexOf: function indexOf()
join: function join()
keys: function keys()
lastIndexOf: function lastIndexOf()
length: Getter
map: function map()
reduce: function reduce()
reduceRight: function reduceRight()
reverse: function reverse()
set: function set()
slice: function slice()
some: function some()
sort: function sort()
subarray: function subarray()
toLocaleString: function toLocaleString()
toString: function toString()
values: function values()
Set && WeakSet
- 4/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
//
//
//
JavaScript TomeXXII
Symbol(Symbol.iterator): function values()
Symbol(Symbol.toStringTag): Getter
<prototype>: {…}
t = "";
for (let e of st) {
t += " => " + e + " : " + e % 2 + "\n";
}
console.log("Impaire ?\n", t);
// Impaire ?
//
=> 14 : 0
//
=> [object Object] : NaN
//
=> 19 : 1
//
=> Inco : NaN
//
=> 14,15,10,14,15 : NaN
t = "";
for (let e of fl) {
t += " => " + e + " : " + e % 2 + "\n";
}
console.log("Impaire ?\n", t);
// Impaire ?
//
=> 14 : 0
//
=> NaN : NaN
//
=> 19 : 1
//
=> 14 : 0
//
=> NaN : NaN
//
=> NaN : NaN
</script>
Exécution avec Firefox :
Exécution avec Yandex :
Set && WeakSet
- 5/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
Les propriétés de « Set » sont les suivantes :
Set && WeakSet
- 6/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
Set && WeakSet
- 7/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
<script type="text/javascript"> "use strict";
var set = new Set(
['azui', 'motuka', 'bilamba', 'azui', 'motuka', 'keba']
);
console.log(set);
// Set(4) [ "azui", "motuka", "bilamba", "keba" ]
// Set(4)
//
size: 7
//
<entries>
//
0: "azui"
//
1: "motuka"
//
2: "bilamba"
//
3: "keba"
//
<prototype>: Object { ... }
set.add('dendo').add('bilamba').add('dendo');
console.log(set);
// Set(5) [ "azui", "motuka", "bilamba", "keba", "dendo" ]
set.delete('boni');
console.log(set);
// Set(5) [ "azui", "motuka", "bilamba", "keba", "dendo" ]
set.delete('bilamba');
console.log(set);
// Set(4) [ "azui", "motuka", "keba", "dendo" ]
console.log("set.size = ", set.size);
// set.size = 4
console.log("set.has('dendon') = ",
set.has('dendon'));
// set.has('dendon') = false
console.log("set.has('dendo') = ", set.has('dendo'));
// set.has('dendo') = true
//
//
//
//
set.forEach(function (it) {
console.log(it);
azui
motuka
keba
dendo
Set && WeakSet
- 8/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
});
</script>
Une application de « Set » c’est la gestion des mots-clés, qui ne doivent
figurer qu’une seule fois dans la liste. De là vous n’avez qu’à faire un « copier-coller » légal et légitime :
<script type="text/javascript"> "use strict";
let s = new Set([
"destructuring", "destructuration", "map", "every",
"filter", "find", "fndIndex", "destructuring",
"forEach", "destructuring", "from", "map",
"indexOf", "isArray", "join", "lastIndexOf",
"destructuring", "length", "map", "splice",
"destructuring", "includes", "map", "push", "of",
"pop", "concat", "destructuring", "reduce",
"reduceRight", "reverse", "shift", "slice", "some",
"sort", "delete", "destructuring", "unshift", map",
"Array", "destructuring", "objet global",
"destructuring", "constructeur", "tableaux",
"destructuring", "objets de haut-niveau",
"javascript", "ecmascript", "name", "map",
"destructuring", "prototype"]);
let arr= s, t="";
for(let i of arr){
t+=i+", "
}
console.log(t);
/*******/
let r = Array.from(s);
t="";
r.forEach(x => t+=x+", ")
console.log(t);
</script>
Set && WeakSet
- 9/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
Gestion du contenu d’un « Set » :
<script type="text/javascript"> "use strict";
var set = new Set(
[94, "string", [25, 93], 50, "string", 94, [25, 93]]
);
console.log(set); //
// Set(5) [ 94, "string", (2) […], 50, (2) […] ]
//
size: 5
//
<entries>
//
0: 94
//
1: "string"
//
2: Array [ … ]
//
2: (2) […]
//
0: 25
//
1: 93
//
length: 2
//
<prototype>: Array []
//
3: 50
//
4: Array [ … ]
//
4: (2) […]
//
0: 25
//
1: 93
Set && WeakSet
- 10/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
//
//
//
length: 2
<prototype>: Array []
<prototype>: Object { … }
console.log(Array.from(set));
// Array(4) [ 94, (2) […], 50, (2) […] ]
// (4) […]
//
0: 94
//
1: "string"
//
2: Array [ 25, 93 ]
//
2: (2) […]
//
0: 25
//
1: 93
//
length: 2
//
<prototype>: Array []
//
3: 50
//
4: Array [ 25, 93 ]
//
4: (2) […]
//
0: 25
//
1: 93
//
length: 2
//
<prototype>: Array []
//
length: 5
//
<prototype>: Array []
let t="";
for (let v of set) t+=v + ` | `;
console.log(t);
// 94 | string | 25,93 | 50 | 25,93 |
// Il n'a pas supprimé le deuxième [25, 93],
// mais il a supprimé le doublon de « 94 »
// et celui de « string ».
let k="",v="";
for (var [cle, val] of set.entries()) {
k+=cle + " | " ;
v+=val + " | " ;
}
console.log(k);
// 94 | string | 25,93 | 50 | 25,93 |
k="";
Set && WeakSet
- 11/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
for (var cle of set.keys()) k+= cle + " | ";
console.log(k);
// 94 | string | 25,93 | 50 | 25,93 |
v="";
for (var val of set.values()) v+= val + " | ";
console.log(v);
// 94 | string | 25,93 | 50 | 25,93 |
</script>
Set && WeakSet
- 12/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
LES « WEAKSETS » :
<script type="text/javascript"> "use strict";
var a = ["jmois", 9, 12], b = ["annee", 2018], c = [10];
// Définition 3 variables distinctes.
// a <= array à 3 éls : stg, num, num
// b <= array à 2 éls : stg et num
// c <= array à un seul él numérique
console.log("a = ", a); // a = Array(3) [ "jmois", 9, 12 ]
console.log("b = ", b); // b = Array [ "annee", 2018 ]
console.log("c = ", c); // c = Array [ 10 ]
var wset = new WeakSet([a, b, a]);
// Constitution d'un « weakSet » en
// proposant deux fois l'array « a »,
// qui ne sera retenu qu'une seule fois.
console.log("wset = ", wset);
// wset = WeakSet [ (3) […], (2) […] ]
wset.add(c).add(b).add(c);
// Ajout d'éléments à la weakSet,
// « b » faisant déjà partie de « wset »,
// « c » proposé deux fois et ne faisant
// pas déjà partie de « wset ».
console.log("wset = ", wset);
// wset = WeakSet(3) [ (1) […], (3) […], (2) […] ]
console.log(".!. Attention .!.");
console.log(`wset.has([10]) = `, wset.has([10]));
// wset.has([10]) = false
console.log("wset.has(c) = ", wset.has(c));
// wset.has(c) = true
wset.delete(c);
console.log("wset.has(c) = ", wset.has(c));
// wset.has(c) = false
console.log("wset = ", wset);
// wset = WeakSet [ (3) […], (2) […] ]
</script>
Set && WeakSet
- 13/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
Avec Firefox Quantm 64.0b13 :
Set && WeakSet
- 14/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript TomeXXII
Avec Yandex Version 18.11.1.385 beta :
Set && WeakSet
- 15/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
Différents types d’Arrays
JavaScript Tome-XXII
- 16/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
Si vous remplacez « weakSet » par « Set » vous obtiendrez le résultat suivant :
<script type="text/javascript"> "use strict";
var a = ["jmois", 9, 12], b = ["annee", 2018], c = [10];
// Définition 3 variables distinctes.
// a <= array à 3 éls : stg, num, num
// b <= array à 2 éls : stg et num
// c <= array à un seul él numérique
console.log("a = ", a); // a = Array(3) [ "jmois", 9, 12 ]
console.log("b = ", b); // b = Array [ "annee", 2018 ]
console.log("c = ", c); // c = Array [ 10 ]
var set = new Set([a, b, a]);
// Constitution d'un « weakSet » en
// proposant deux fois l'array « a »,
// qui ne sera retenu qu'une seule fois.
console.log("set = ", set);
// set = WeakSet [ (3) […], (2) […] ]
set.add(c).add(b).add(c);
Différents types d’Arrays
- 17/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
// Ajout d'éléments à la weakSet,
// « b » faisant déjà partie de « set »,
// « c » proposé deux fois et ne faisant
// pas déjà partie de « set ».
console.log("set = ", set);
// set = WeakSet(3) [ (1) […], (3) […], (2) […] ]
console.log(".!. Attention .!.");
console.log(`set.has([10]) = `, set.has([10]));
// set.has([10]) = false
console.log("set.has(c) = ", set.has(c));
// set.has(c) = true
set.delete(c);
console.log("set.has(c) = ", set.has(c));
// set.has(c) = false
console.log("set = ", set);
// set = WeakSet [ (3) […], (2) […] ]
</script>
<!--script type="text/javascript"> "use strict";
var a = [1] , b = [2] , c = [3];
var wmap = new WeakMap([[a, 1], [b, 2]]);
wmap.set(c, 3).set(b, 4);
console.log(wmap.has(a));
console.log(wmap.has([1]));
console.log(wmap.get(a));
wmap.delete(a);
console.log(wmap.get(a));
// => true
// => false
// => 1
// => undefined
// Private properties store:
var Person = (function () {
var names = new WeakMap;
function Person(name) {
names.set(this, name);
}
Person.prototype.getName = function () {
return names.get(this);
Différents types d’Arrays
- 18/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
};
return Person;
})();
JavaScript Tome-XXII
var person = new Person('Vasya');
console.log(person.getName());
// => 'Vasya'
for (var key in person)
console.log(key); // => only 'getName'
</script>
Kinshasa, le jeudi, 4. avril 2019 (10:48 ).
Différents types d’Arrays
- 19/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
Mots-clés :
Arrays typées, ArrayBuffer, Set, String, Typed Array, Array Typée,
Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array,
Int32Array, Float32Array, Float64Array, Arrays, Array, slice, subarray,
constructeur, initialiseur, Object literal, littéral d’objet, entries, values,
paradigme
DIASOLUKA Nz. Luyalu
Docteur en Médecine, Chirurgie & Accouchements
(1977),
CNOM : 0866 - Spécialiste en ophtalmologie (1980)
Études humanités : Scientifique - Mathématiques &
Physique.
Informaticien-amateur, Programmeur et WebMaster.
Chercheur indépendant, autonome et autofinancé,
bénévole, sans aucun conflit d’intérêt ou
liens d'intérêts ou contrainte promotionnelle
avec qui qu’il soit ou quelqu’organisme ou
institution / organisation que ce soit,
étatique, paraétatique ou privé, industriel ou
commercial en relation avec le sujet présenté.
+243 - 851278216 - 899508675 - 991239212 - 902263541 - 813572818
[email protected]
Différents types d’Arrays
- 20/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
Autre Lecture :
https://www.scribd.com/document/374738470/Le-Plus-Grand-Secret-de-LaCreation
D’autres publications pouvant aussi intéresser :
• https://www.scribd.com/document/377036251/LeDosage-Des-Medicaments-en-Cac-Cas
• https://www.scribd.com/document/377035454/LeHasard-Des-Thermometres-Non-contact-a-Infrarouge
• https://www.scribd.com/document/376222482/PetiteIntroduction-Aux-Fonctions-JavaScript
• https://www.scribd.com/document/376221919/La-Foien-Jesus-Christ-Pour-Quoi-Faire
• https://www.scribd.com/document/375689778/Lacuitevisuelle-angulaire
• https://www.scribd.com/document/375349851/Lavariable-This
•
https://www.scribd.com/document/375024162/FonctionsImbriquees-en-JS
• https://www.scribd.com/document/374789297/FormatInterne-Des-Objets-JavaScript
•
https://www.scribd.com/document/374788758/Iterationsen-JavaScript
• https://www.scribd.com/document/374738470/Le-PlusGrand-Secret-de-La-Creation
Différents types d’Arrays
- 21/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
• https://www.scribd.com/document/374597969/NouvelleFormule-d-IMC-indice-de-doduite-Selon-Dr-Diasoluka
• https://www.scribd.com/document/373847209/PropertyDescriptors
• https://www.scribd.com/document/373833282/l-ObjetGlobal-Window
•
https://www.scribd.com/document/372665249/JavascriptTome-II
• https://www.scribd.com/document/355291488/motiliteoculaire-2
• https://www.scribd.com/document/355291239/motiliteoculaire-I
• https://www.scribd.com/document/355290248/Script-dAnalyses-Des-Reflexes-Pupillomoteurs
•
https://www.scribd.com/document/321168468/Renseigne
ments-Id-et-Anthropometriques
•
https://www.scribd.com/document/320856721/Emission31-Jul-2016
•
https://www.scribd.com/document/318182982/Complicati
on-Visuelle-du-Traitement-de-La-Malaria
• https://www.scribd.com/document/318180637/RapportEntre-Oxymetrie-Et-Type-Respiration
•
https://www.scribd.com/document/315746265/Classificati
on-Des-Medicaments
Différents types d’Arrays
- 22/23 -
jeudi, 4. avril 2019 (10:48 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-XXII
•
https://www.scribd.com/document/315745909/Incongruen
ces-Heresies-et-Heterodoxies-de-la-Notion-deLaboratoire
• https://www.scribd.com/document/315745725/RapportEntre-Oxymetrie-Et-Type-Respiration
Différents types d’Arrays
- 23/23 -
jeudi, 4. avril 2019 (10:48 )

Documents pareils