ReferenceError

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

Объект ReferenceError представляет ошибку, возникающую при обращении к переменной, которая не существует (или не была инициализирована) в текущей области видимости.

ReferenceError является сериализуемым объектом, поэтому он может быть клонирован с помощью structuredClone() или передан между воркерами с использованием postMessage().

RangeReferenceErrorError является подклассом Error.

Конструктор

ReferenceError()

Создаёт новый объект ReferenceError.

Свойства экземпляра

Также наследует свойства экземпляра своего родителя Error.

Эти свойства определены в ReferenceError.prototype и есть у всех экземпляров ReferenceError.

ReferenceError.prototype.constructor

Функция-конструктор, создающая экземпляр объекта. Для экземпляров ReferenceError начальным значением является конструктор ReferenceError.

ReferenceError.prototype.name

Представляет название типа ошибки. Начальным значением ReferenceError.prototype.name является "ReferenceError".

Методы экземпляра

Наследует методы экземпляра своего родителя Error.

Примеры

Перехват ReferenceError

js
try {
  let a = undefinedVariable;
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message); // "undefinedVariable is not defined"
  console.log(e.name); // "ReferenceError"
  console.log(e.stack); // Стек ошибок
}

Создание ReferenceError

js
try {
  throw new ReferenceError("Привет");
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message); // "Привет"
  console.log(e.name); // "ReferenceError"
  console.log(e.stack); // Стек ошибок
}

Спецификации

Specification
ECMAScript® 2025 Language Specification
# sec-native-error-types-used-in-this-standard-referenceerror

Совместимость с браузерами

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
ReferenceError
ReferenceError() constructor
ReferenceError is serializable

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

Смотрите также