Miscellaneous-Miscellanées - javascript tome viii

publicité
J.B. Dadet DIASOLUKA Luyalu Nzoyifuanga
+243 - 851278216 - 899508675 - 991239212 - 902263541 - 813572818
CHAPITRE 14 : Une propriété (plutôt méthode) particulière : Symbol()
Permet en quelque sorte de créer des variables spéciales ayant des valeurs littérales et qui ne se mêlent pas avec les variables ordinaires (par
exemple dans l’indexation des arrays).
Exemple :
<script type="text/javascript"> "use strict";
var
idxv = Symbol('qcmV');
console.log(idxv);
// Symbol(qcmV)
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
const idxc = Symbol('qcmC');
console.log(idxc);
// Symbol(qcmC)
const a = [68, 80, 55];
console.log(a, ` , a.length = ${a.length}`);
// Array(3) [ 68, 80, 55 ] , a.length = 3
a[idxv]= 71;
console.log(a, ` , a.length = ${a.length}`);
// Array(3) [ 68, 80, 55 ] , a.length = 3
console.log(`a[idxv] = `, a[idxv]);
// a[idxv] = 71
a[idxv]= 42;
// Écrasement de a[idxv]
console.log(a, ` , a.length = ${a.length}`);
// Array(3) [ 68, 80, 55 ] , a.length = 3
console.log(`a[idxv] = `, a[idxv]);
// a[idxv] = 42
a[idxc]= 29;
console.log(a, ` , a.length = ${a.length}`);
// Array(3) [ 68, 80, 55 ] , a.length = 3
console.log(`a[idxc] = `, a[idxc]);
// a[idxc] = 29
a[idxc]= 46;
// Écrasement de a[idxc]
console.log(a, ` , a.length = ${a.length}`);
// Array(3) [ 68, 80, 55 ] , a.length = 3
console.log(`a[idxc] = `, a[idxc]);
// a[idxc] = 46
a[a.length]= 95;
console.log(a, ` , a.length = ${a.length}`);
// Array(4) [ 68, 80, 55, 95 ] , a.length = 4
console.log(`a[a.length] = `, a[a.length]);
Miscellaneous
- 2 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
// a[a.length] =
JavaScript Tome-VIII
undefined
console.log(`a[a.length] = , ${a[a.length]}`);
// a[a.length] = undefined
console.log(`a[a.length] = , a${[a.length]}`);
// a[a.length] = undefined
console.log(`a[4] = `, a[4]);
// a[4] = undefined
console.log(`a[idxv] = `, a[idxv]);
// a[idxv] = 42
console.log(`a[Symbol('1')] = `, a[Symbol('1')]);
// a[Symbol('1')] = undefined
console.log(`a["Symbol('1')"] = `, a["Symbol('1')"]);
// a["Symbol('1')"] = undefined
console.log(`a[Symbol(1)] = `, a[Symbol(1)]);
// a[Symbol(1)] = undefined
console.log(`a["Symbol(1)"] = `, a["Symbol(1)"]);
// a["Symbol(1)"] = undefined
console.log(`(a[eval(Symbol('1'))]) = `,
(a[eval(Symbol('1'))]));
// => (a[eval(Symbol('1'))]) = undefined
console.log(`eval(a[(Symbol('1'))]) = `,
eval(a[(Symbol('1'))]));
// => eval(a[(Symbol('1'))]) = undefined
console.log(`Symbol.keyFor(idxv) = `,
Symbol.keyFor(idxv));
// => Symbol.keyFor(idxv) = undefined
console.log(`a[(Symbol.keyFor(idxv))] = `,
a[(Symbol.keyFor(idxv))]);
// => a[(Symbol.keyFor(idxv))] = undefined
console.log(`Object.keys(a) = ${Object.keys(a)}`);
Miscellaneous
- 3 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
// => Object.keys(a) = 0,1,2,3
console.log(`Object.getOwnPropertyNames(a) = `,
Object.getOwnPropertyNames(a));
// => Array(5) [ "0", "1", "2", "3", "length"
]
console.log(`Object.getOwnPropertySymbols(a) = `,
Object.getOwnPropertySymbols(a));
// => Array [ Symbol(qcmV), Symbol(qcmC) ]
console.log(`Reflect.ownKeys(a) = `,
Reflect.ownKeys(a));
// Array(7) [ "0", "1", "2", "3", "length", Symbol(qcmV), Symbol(qcmC) ]
console.log(Object.entries(a));
// Array(4) [ (2) […], (2) […], (2) […], (2) […] ]
</script>
Exemple dans une fonction :
Création, utilisation, contraintes et restrictions.
Miscellaneous
- 4 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
<script type="text/javascript"> "use strict";
var Elenge = (function () {
var KOMBO = Symbol('kombo');
var MBOKA = Symbol('mboka');
function Elenge(kombo, pName) {
this[KOMBO] = kombo;
this[MBOKA] = pName;
}
Elenge.prototype.dispKombo = function () {
console.log(
"=> Dans Elenge.prototype.dispKombo");
return this[KOMBO]; // Lors de l'appel
};
Elenge.prototype.dispMboka = function () {
console.log(
"=> Dans Elenge.prototype.dispMboka");
return this[MBOKA]; // Lors de l'appel
};
return Elenge;
})();
// Lors de l'instanciation
console.log("Départ");
var motu = new Elenge('Kele', 'Lobiko');
console.log(motu.dispKombo(), motu.dispMboka());
// => Kele Lobiko
let t = "";
for (var key in motu) t += key + ` | `
console.log(t); // => dispKombo | dispMboka |
// Les Symbols ne sont pas énumerables
console.log(motu["kombo"]);
console.log(motu["KOMBO"]);
// => undefined
// => undefined
// Les symboles sont uniques
console.log(motu[Symbol('kombo')]);
console.log(motu[Symbol('KOMBO')]);
// => undefined
// => undefined
console.log(Object.getOwnPropertySymbols(Elenge));
Miscellaneous
- 5 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
// => Array []
</script>
Dommage, l’Array des « Symbol » est vide..
Miscellaneous
- 6 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Exemple dans un object :
<script type="text/javascript"> "use strict";
var O = {annee: 2018};
Object.defineProperty(O, 'mois', {value: 12});
O[Symbol('jour')] = 8;
O[Symbol('heure')] = Date.now;
console.log(`Object.keys(O) = ${Object.keys(O)}`);
// => annee
console.log(`Object.getOwnPropertyNames(O) = `,
Object.getOwnPropertyNames(O));
// => Array [ "annee", "mois" ]
console.log(`Object.getOwnPropertySymbols(O) = `,
Object.getOwnPropertySymbols(O));
// => Array [ Symbol(jour) ]
console.log(`Reflect.ownKeys(O) = `,
Reflect.ownKeys(O));
// => Array(3) [ "annee", "mois", Symbol(jour)
]
</script>
Miscellaneous
- 7 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Quelques méthodes de « Symbol » :
<script type="text/javascript"> "use strict";
var Elenge = (function () {
var KOMBO = Symbol('kombo');
var MBOKA = Symbol('mboka');
function Elenge(kombo, pName) {
this[KOMBO] = kombo;
this[MBOKA] = pName;
}
Elenge.prototype.dispKombo = function () {
console.log(
"=> Dans Elenge.prototype.dispKombo");
return this[KOMBO]; // Lors de l'appel
Miscellaneous
- 8 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
};
Elenge.prototype.dispMboka = function () {
console.log(
"=> Dans Elenge.prototype.dispMboka");
return this[MBOKA]; // Lors de l'appel
};
return Elenge;
})();
// Lors de l'instanciation
console.log("Départ");
var motu = new Elenge('Kele', 'Lobiko');
console.log(motu.dispKombo(), motu.dispMboka());
// => Kele Lobiko
console.log(motu["kombo"]);
console.log(motu["KOMBO"]);
// => undefined
// => undefined
// Les symboles sont uniques
console.log(motu[Symbol('kombo')]); // => undefined
console.log(motu[Symbol('KOMBO')]); // => undefined
console.log(Object.getOwnPropertySymbols(Elenge));
// => Array []
var t = "";
for (var key in motu) t += key + ` | `;
console.log(t); // => dispKombo | dispMboka |
// Les Symbols ne sont pas énumerables
var keysymb = Symbol.for("motu");
console.log(`« keysymb === Symbol.for("motu") » = ` ,
keysymb === Symbol.for("motu")); // true
console.log(`keysymb = ` , keysymb); // Symbol(motu)
console.log(`Symbol.for("motu") = `,
Symbol.for("motu")); // Symbol(motu)
console.log(`Symbol.keyFor(keysymb) = `,
Symbol.keyFor(keysymb)); // motu
var t="";
Miscellaneous
- 9 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
for (var y in Symbol.keyFor(keysymb))
t += y + ` | `;
console.log(t); // => 0 | 1 | 2 | 3 |
var t="";
for (var i in Symbol.keyFor(keysymb))
t += Symbol.keyFor(keysymb)[i] + ` | `;
console.log(t); // => m | o | t | u |
</script>
Miscellaneous
- 10 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
CHAPITRE 11 : LES PROPRIÉTÉS ET LEURS ATTRIBUTS:
Les propriétés sont des objets dont les propriétés sont leurs attributs.
Sur https://www.ecma-international.org/ecma-262/8.0 nous lisons ceci :
6.1.7.1 Property Attributes
Attributes are used in this specification to define and explain the state of
Object properties. A data property associates a key value with the attributes listed in Table 2.
Table 2: Attributes of a Data
Property Attribute Name
Value Domain
Description
[[Value]]
Any
The value retrieved by a get access of
ECMAScript the property.
language
type
[[Writable]]
Boolean
Peut-être modifié [directement].
If false, attempts by ECMAScript code
to change the property's [[Value]] attribute using [[Set]] will not succeed.
[[Enumerable]] Boolean
Invisible dans certains contextes.
If true, the property will be enumerated
by a for-in enumeration (see 13.7.5).
Otherwise, the property is said to be
non-enumerable.
[[Configurable]] Boolean
Peut être supprimée, ou
Son « property descriptor » changé.
If false, attempts to delete the property,
change the property to be an accessor
property, or change its attributes (other
Miscellaneous
vendredi, 31. mai 2019 (7:52 )
- 11 / 19 -
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
than [[Value]], or changing [[Writable]]
to false) will fail.
Comme dit ci-dessus, [[value]] est la valeur que vous attribuez ou attendez de l’objet :
<script type="text/javascript"> "use strict";
// Définition de propriété lors de la création
var obj = {nom:"nomEl",matr:45};
console.log(Object.getOwnPropertyDescriptors(obj));
// Object { nom: {…}, matr: {…} }
// {…}
//
matr: Object { value: 45, writable: true, enumerable:
true, … }
//
nom: Object { value: "nomEl", writable: true, enumerable: true, … }
// {…}
// matr: {…}
//
configurable: true
//
enumerable: true
//
value: 45
//
writable: true
// nom: {…}
//
configurable: true
//
enumerable: true
//
value: "nomEl"
//
writable: true
//
console.log(obj["nom"], obj.matr);
nomEl
45
// [Re]Définition brève avec defineProperty
Object.defineProperty(obj, 'defProp', {
value: "definedProp" });
console.log(obj["nom"], obj['matr'], obj.matr,
obj.defProp);
//
nomEl
45
45
definedProp
console.log(Object.getOwnPropertyDescriptor(obj,"nom"));
Miscellaneous
- 12 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
// Object { value: "nomEl", writable: true, enumerable:
true, configurable: true }
// {…}
//
configurable: true
//
enumerable: true
//
value: "nomEl"
//
writable: true
console.log(Object.getOwnPropertyDescriptor(
obj,"defProp"));
// Object { value: "definedProp", writable: false, enumerable: false, configurable: false }
// {…}
//
configurable: false
//
enumerable: false
//
value: "definedProp"
//
writable: false
</script>
Ci-dessous, voici comment on peut manipuler ces attributs :
1er Définir une propriété : avec la méthode Object.defineProperty
Object.defineProperty([objet], [property-key], { value: [property-value] })
[] = à fournir.
2e Lire une ou les propriété(s) : .avec la méthode Object.getOwnPropertyDescriptors.
Object.getOwnPropertyDescriptors([objet])
On peut définir les propriétés d’un objet [et éventuellement les attributs
de ces propriétés] de plusieurs façons ;
<script type="text/javascript"> "use strict";
// Donne aussi les valeurs par défaut des attributs des
// propriétés selon les différents modes de définition.
Miscellaneous
- 13 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
// Définition de propriété lors de la création
var obj = {nom:"nomEl",matr:45};
// Définition directe de propriété à partir
// du monde externe
obj['dirProp']="Direct Property";
// [Re]Définition brève avec defineProperty
Object.defineProperty(obj, 'defProp', { value: "definedProp" });
// Modèle de [Re]Définition étendue avec defineProperty
// mécanisme interne.
Object.defineProperty(obj, 'defPropx', { value: "definedProp Extended",
configurable: true });
// On peut aussi utiliser getter et setter.
console.dir("obj.nom=",obj.nom); // obj.nom= nomEl
console.dir("obj.matr=",obj.matr); // obj.matr= 45
console.dir("obj.dirProp=",obj.dirProp);
// obj.dirProp= Direct Property
// Au lieu des points on peut aussi utiliser
// des crochets.
console.dir("obj['defProp']=",obj.defProp);
// obj['defProp']= definedProp
console.dir("obj.defPropx=",obj.defPropx);
// obj.defPropx= definedProp Extended
console.log(Object.getOwnPropertyDescriptors(obj))
/* Object { nom: {…}, matr: {…}, dirProp: {…}, defProp:
{…}, defPropx: {…} }
defProp: Object { value: "definedProp", writable:
false, enumerable: false, … }
defPropx: Object { value: "definedProp Extended", writable: false, enumerable: false, … }
dirProp: Object { value: "Direct Property", writable:
true, enumerable: true, … }
matr: Object { value: 45, writable: true, enumerable:
true, … }
nom: Object { value: "nomEl", writable: true, enumera-
Miscellaneous
- 14 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
ble: true, … }
defProp: {…}
configurable: false
enumerable: false
value: "definedProp"
writable: false
defPropx: {…}
configurable: true
enumerable: false
value: "definedProp Extended"
writable: false
dirProp: {…}
configurable: true
enumerable: true
value: "Direct Property"
writable: true
matr: {…}
configurable: true
enumerable: true
value: 45
writable: true
nom: {…}
configurable: true
enumerable: true
value: "nomEl"
writable: true
*/
</script>
Miscellaneous
- 15 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Kinshasa, le vendredi 31 mai 2019 - 7:52:51 PM
Mots-clés :
JAVASCRIPT, Programmation Internet, keys, values, parseInt, parseFloat,
toString, fonction fléchée, sloppy mode, mode strict, prototype, objet ordinaire, objet exotique, objet standard, built-in object, Scope, contexte
d’exécution, Domaine, Portée, Étendue, Visibilité, Accessibilité, durée de
vie, Es10, ECMASCRIPT 2019, LiveScript, extra-dimensionnels, entités
éthériques non-biologiques, TC39, ECMA, Kaprekar
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]
Autre Lecture :
https://www.scribd.com/document/374738470/Le-Plus-Grand-Secret-de-La-
Miscellaneous
- 16 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Creation
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
• https://www.scribd.com/document/374597969/NouvelleFormule-d-IMC-indice-de-doduite-Selon-Dr-Diasoluka
• https://www.scribd.com/document/373847209/PropertyMiscellaneous
- 17 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Descriptors
• 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
•
https://www.scribd.com/document/315745909/Incongruen
ces-Heresies-et-Heterodoxies-de-la-Notion-deMiscellaneous
- 18 / 19 -
vendredi, 31. mai 2019 (7:52 )
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Laboratoire
• https://www.scribd.com/document/315745725/RapportEntre-Oxymetrie-Et-Type-Respiration
Miscellaneous
- 19 / 19 -
vendredi, 31. mai 2019 (7:52 )
Téléchargement