ReferenceError
        
        
          
                Baseline
                
                  Widely available
                
                 *
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
* Some parts of this feature may have varying levels of support.
ReferenceError オブジェクトは、現在のスコープに存在しない(あるいはまだ初期化されていない)変数が参照されたときのエラーを表します。
ReferenceError はシリアライズ可能オブジェクトなので、 structuredClone() で複製したり、ワーカー間で postMessage() を使用してコピーしたりすることができます。
ReferenceError は Error のサブクラスです。
コンストラクター
ReferenceError()- 
新しい
ReferenceErrorオブジェクトを生成します。 
インスタンスプロパティ
親である Error から継承したプロパティもあります。
これらのプロパティは ReferenceError.prototype で定義されており、すべての ReferenceError インスタンスで共有されます。
ReferenceError.prototype.constructor- 
このインスタンスオブジェクトを作成したコンストラクター関数。
ReferenceErrorインスタンスの場合、初期値はReferenceErrorコンストラクタです。 ReferenceError.prototype.name- 
エラー型の名前を表します。
ReferenceError.prototype.nameの初期値は"ReferenceError"です。 
インスタンスメソッド
親である Error から継承したメソッドもあります。
例
>ReferenceError の捕捉
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 の生成
try {
  throw new ReferenceError("Hello");
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "ReferenceError"
  console.log(e.stack); // このエラーのスタック
}
仕様書
| Specification | 
|---|
| ECMAScript® 2026 Language Specification> # sec-native-error-types-used-in-this-standard-referenceerror>  | 
            
ブラウザーの互換性
Loading…