From 39f2114f9797eb51994966c6bb8ff1814c9a4da8 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:36:08 +0100 Subject: unslug fr: move --- .../global_objects/function/tostring/index.html | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 files/fr/web/javascript/reference/global_objects/function/tostring/index.html (limited to 'files/fr/web/javascript/reference/global_objects/function/tostring') diff --git a/files/fr/web/javascript/reference/global_objects/function/tostring/index.html b/files/fr/web/javascript/reference/global_objects/function/tostring/index.html new file mode 100644 index 0000000000..db667ff0f6 --- /dev/null +++ b/files/fr/web/javascript/reference/global_objects/function/tostring/index.html @@ -0,0 +1,98 @@ +--- +title: Function.prototype.toString() +slug: Web/JavaScript/Reference/Objets_globaux/Function/toString +tags: + - Function + - JavaScript + - Méthode + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Function/toString +--- +
{{JSRef}}
+ +

La méthode toString() renvoie une chaîne de caractères représentant le code source de la fonction.

+ +
{{EmbedInteractiveExample("pages/js/function-tostring.html")}}
+ + + +

Syntaxe

+ +
function.toString(indentation)
+ +

Valeur de retour

+ +

Une chaîne de caractères qui représente le code source de la fonction.

+ +

Description

+ +

L'objet {{jsxref("Function")}} redéfinit la méthode {{jsxref("Object.prototype.toString", "toString")}} de l'objet {{jsxref("Object")}} ; il n'hérite donc pas de {{jsxref("Object.prototype.toString")}}. Pour les objets {{jsxref("Function")}}, la méthode toString renvoie une chaîne de caractères représentant l'objet sous la forme d'une déclaration de fonction. Pour ce faire, toString décompile la fonction pour renvoyer une chaîne qui contient le mot-clé function, la liste des arguments, les accolades et la source correspondant au corps de la fonction.

+ +

Le moteur JavaScript appelle la méthode toString automatiquement lorsqu'un objet {{jsxref("Function")}} doit être représenté textuellement (par exemple lorsqu'une fonction doit être concaténée avec une chaîne de caractères).

+ +

La méthode toString() lèvera une exception {{jsxref("TypeError")}} (« Function.prototype.toString called on incompatible object ») si la valeur this n'est pas un objet Function.

+ +
Function.prototype.toString.call("toto"); // TypeError
+
+ +

Si la méthode toString() est appelée sur des fonctions natives qui ne sont pas définies dans le script, toString() renvoie une chaîne de caractères indiquant le caractère natif :

+ +
Math.abs.toString();
+
+"function abs() {
+    [native code]
+}"
+ +

Si la méthode toString() est appelée sur une fonction créée avec le constructeur Function, toString() renverra le code source d'une fonction intitulée anonymous et utilisera les paramètres et le corps de la fonction fournis.

+ +

Spécifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpécificationÉtatCommentaires
{{SpecName('ES1')}}{{Spec2('ES1')}}Définition initiale. Implémentée avec JavaScript 1.1.
Function.prototype.toString()BrouillonStandardise la chaîne de caractères utilisée pour les fonctions natives ainsi que la gestion des fins de ligne.
{{SpecName('ES6', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}{{Spec2('ES6')}}Critères supplémentaires ajoutés sur la représentation de la chaîne.
{{SpecName('ESDraft', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}{{Spec2('ESDraft')}} 
+ +

Compatibilité des navigateurs

+ + + +

{{Compat("javascript.builtins.Function.toString")}}

+ +

Notes spécifiques à Firefox

+ + + +

Voir aussi

+ + -- cgit v1.2.3-54-g00ecf