Baseline Widely available
La méthode toString()
renvoie une chaîne de caractères représentant l'objet Symbol
.
console.log(Symbol("desc").toString());
// Expected output: "Symbol(desc)"
console.log(Symbol.iterator.toString());
// Expected output: "Symbol(Symbol.iterator)
console.log(Symbol.for("foo").toString());
// Expected output: "Symbol(foo)"
// console.log(Symbol('foo') + 'bar');
// Expected output: Error: Can't convert symbol to string
Syntaxe Valeur de retour
Une chaîne de caractères qui représente l'objet Symbol
.
L'objet Symbol
surcharge la méthode toString()
d'Object
et n'hérite pas de Object.prototype.toString()
. Pour les objets Symbol
, la méthode toString()
renvoie représentation de l'objet sous forme d'une chaîne de caractères.
Bien qu'il soit possible d'appeler toString()
pour les symboles, il n'est pas possible de concaténer une chaîne de caractères avec ce type d'objet :
Symbol("toto") + "machin"; // TypeError : Impossible de convertir un symbole en chaîne de caractères
Exemples
Symbol("desc").toString(); // "Symbol(desc)"
// symboles connus
Symbol.iterator.toString(); // "Symbol(Symbol.iterator)
// symboles globaux
Symbol.for("toto").toString(); // "Symbol(toto)"
Spécifications Compatibilité des navigateurs Voir aussi
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4