Miscellaneous-Miscellanées - javascript tome viii

J.B. Dadet DIASOLUKA Luyalu Nzoyifuanga
+243 - 851278216 - 899508675 - 991239212 - 902263541 - 813572818
CHAPITRE 14 : Une propriété (plutôt méthode) particulière : Sym-
bol()
Permet en quelque sorte de créer des variables spéciales ayant des va-
leurs littérales et qui ne se mêlent pas avec les variables ordinaires (par
exemple dans lindexation 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
Miscellaneous - 2 / 19 - vendredi, 31. mai 2019 (7:52 )
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]);
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Miscellaneous - 3 / 19 - vendredi, 31. mai 2019 (7:52 )
// a[a.length] = 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)}`);
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Miscellaneous - 4 / 19 - vendredi, 31. mai 2019 (7:52 )
// => 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", Sym-
bol(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.
J.D.B. DIASOLUKA Nz. Luyalu
JavaScript Tome-VIII
Miscellaneous - 5 / 19 - vendredi, 31. mai 2019 (7:52 )
<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"]); // => undefined
console.log(motu["KOMBO"]); // => undefined
// Les symboles sont uniques
console.log(motu[Symbol('kombo')]); // => undefined
console.log(motu[Symbol('KOMBO')]); // => undefined
console.log(Object.getOwnPropertySymbols(Elenge));
1 / 19 100%

Miscellaneous-Miscellanées - javascript tome viii

La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !