aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/referenceerror/index.html
blob: 7e9f25a05139ad4df9aefa2c945ca5e0a502da39 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
title: ReferenceError
slug: Web/JavaScript/Reference/Global_Objects/ReferenceError
tags:
  - Error
  - JavaScript
  - Object
  - Reference
  - ReferenceError
translation_of: Web/JavaScript/Reference/Global_Objects/ReferenceError
original_slug: Web/JavaScript/Reference/Objets_globaux/ReferenceError
---
<div>{{JSRef}}</div>

<p>L'objet <code><strong>ReferenceError</strong></code> représente une erreur qui se produit lorsqu'il est fait référence à une variable qui n'existe pas.</p>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox">new ReferenceError(<var>[message[, nomFichier[, numLigne]]]</var>)</pre>

<h3 id="Paramètres">Paramètres</h3>

<dl>
 <dt><code>message</code></dt>
 <dd>Paramètre optionnel. Une description de l'erreur, lisible par un être humain.</dd>
 <dt><code>nomFichier</code> {{Non-standard_inline}}</dt>
 <dd>Paramètre optionnel. Le nom du fichier qui contient le code à l'origine de l'exception.</dd>
 <dt><code>numLigne</code> {{Non-standard_inline}}</dt>
 <dd>Paramètre optionnel. Le numéro de ligne dans le fichier qui contient le code à l'origine de l'exception.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>Une exception <code>ReferenceError</code> est lancée quand on tente de faire référence à une variable qui n'a pas été déclarée.</p>

<h2 id="Propriétés">Propriétés</h2>

<dl>
 <dt>{{jsxref("ReferenceError.prototype")}}</dt>
 <dd>Cette propriété permet d'ajouter des propriétés à un objet <code>ReferenceError</code>.</dd>
</dl>

<h2 id="Méthodes">Méthodes</h2>

<p>L'objet global <code>ReferenceError</code> ne contient aucune méthode qui lui soit propre. En revanche, il hérite de certaines méthodes via l'héritage et sa chaîne de prototypes.</p>

<h2 id="Instances_de_ReferenceError">Instances de <code>ReferenceError</code></h2>

<h3 id="Propriétés_2">Propriétés</h3>

<div>{{page('fr/docs/Web/JavaScript/Reference/Objets_globaux/ReferenceError/prototype','Properties')}}</div>

<h3 id="Méthodes_2">Méthodes</h3>

<div>{{page('fr/docs/Web/JavaScript/Reference/Objets_globaux/ReferenceError/prototype','M.C3.A9thodes')}}</div>

<h2 id="Exemples">Exemples</h2>

<h3 id="Intercepter_une_exception_ReferenceError">Intercepter une exception ReferenceError</h3>

<pre class="brush: js">try {
  var a = variableNonDéfinie;
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message);                   // "variableNonDéfinie is not defined"
  console.log(e.name);                      // "ReferenceError"
  console.log(e.fileName);                  // "Scratchpad/1"
  console.log(e.lineNumber);                // 2
  console.log(e.columnNumber);              // 6
  console.log(e.stack);                     // "@Scratchpad/2:2:7\n"
}</pre>

<h3 id="Créer_une_exception_ReferenceError">Créer une exception <code>ReferenceError</code></h3>

<pre class="brush: js">try {
  throw new ReferenceError('Bonjour', 'unFichier.js', 10);
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message);                   // "Bonjour"
  console.log(e.name);                      // "ReferenceError"
  console.log(e.fileName);                  // "unFichier.js"
  console.log(e.lineNumber);                // 10
  console.log(e.columnNumber);              // 0
  console.log(e.stack);                     // "@Scratchpad/2:2:9\n"
}</pre>

<h2 id="Spécifications">Spécifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spécification</th>
   <th scope="col">Statut</th>
   <th scope="col">Commentaires</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Définition initiale.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.11.6.3', 'ReferenceError')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-referenceerror', 'ReferenceError')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-native-error-types-used-in-this-standard-referenceerror', 'ReferenceError')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>

<p>{{Compat("javascript.builtins.ReferenceError")}}</p>

<h2 id="Voir_aussi">Voir aussi</h2>

<ul>
 <li>{{jsxref("Error")}}</li>
 <li>{{jsxref("ReferenceError.prototype")}}</li>
</ul>