aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/error/tostring/index.md
blob: 15df5e7eb6792d40072f67f1f75f25d5d710e4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: Error.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Error/toString
tags:
  - Error
  - JavaScript
  - Méthode
  - Prototype
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Error/toString
original_slug: Web/JavaScript/Reference/Objets_globaux/Error/toString
---
{{JSRef}}

La méthode **`toString()`** renvoie une représentation de l'objet {{jsxref("Error")}} sous la forme d'une chaine de caractères.

## Syntaxe

    e.toString()

### Valeur de retour

Une chaîne de caractères représentant l'objet {{jsxref("Error")}}.

## Description

L'objet {{jsxref("Error")}} surcharge la méthode {{jsxref("Object.prototype.toString()")}} héritée par tous les objets. Sa sémantique est la suivante (en partant du principe que {{jsxref("Object")}} et {{jsxref("String")}} ont leurs valeurs originales) :

```js
Error.prototype.toString = function () {
  "use strict";

  var obj = Object(this);
  if (obj !== this)
    throw new TypeError();

  var name = this.name;
  name = (name === undefined) ? "Error" : String(name);

  var msg = this.message;
  msg = (msg === undefined) ? "" : String(msg);

  if (name === "")
    return msg;
  if (msg === "")
    return name;

  return name + ": " + msg;
};
```

## Exemples

```js
var e = new Error("Erreur fatale");
console.log(e.toString()); // "Error: Erreur fatale"

e.name = undefined;
console.log(e.toString()); // "Error: Erreur fatale"

e.name = "";
console.log(e.toString()); // "Erreur fatale"

e.message = undefined;
console.log(e.toString()); // ""

e.name = "salut";
console.log(e.toString()); // "salut"
```

## Spécifications

| Spécification                                                                                                    | État                         | Commentaires                                          |
| ---------------------------------------------------------------------------------------------------------------- | ---------------------------- | ----------------------------------------------------- |
| {{SpecName('ES1')}}                                                                                         | {{Spec2('ES1')}}         | Définition initiale. Implémentée avec JavaScript 1.1. |
| {{SpecName('ES5.1', '#sec-15.11.4.4', 'Error.prototype.toString')}}                         | {{Spec2('ES5.1')}}     |                                                       |
| {{SpecName('ES6', '#sec-error.prototype.tostring', 'Error.prototype.toString')}}     | {{Spec2('ES6')}}         |                                                       |
| {{SpecName('ESDraft', '#sec-error.prototype.tostring', 'Error.prototype.toString')}} | {{Spec2('ESDraft')}} |                                                       |

## Compatibilité des navigateurs

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

## Voir aussi

- {{jsxref("Error.prototype.toSource()")}} {{non-standard_inline}}