Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

ReferenceError: reference to undefined property "x"

この記事は編集レビューを必要としています。ぜひご協力ください

メッセージ

ReferenceError: reference to undefined property "x" (Firefox)

エラータイプ

strict モード でのみ、ReferenceError の警告が出ます。

何がうまくいかなかったのか?

存在しないオブジェクトのプロパティにアクセスしようとしています。プロパティにアクセスする方法は 2 つあります。詳細については、メンバー演算子参照ページを見てください。

未定義プロパティを参照することによるエラーは、strict モードのコードでのみ発生します。非 strict コードでは、暗黙的に無視されます。

無効なケース

このケースでは、"bar" は未定義のプロパティです。

"use strict"; 

var foo = {};
foo.bar; // ReferenceError: reference to undefined property "bar"

有効なケース

エラーを避けるには、"bar" プロパティを定義するか、使用する前に "bar" プロパティが存在するか確認する必要があります(たとえば、Object.prototype.hasOwnProperty() メソッドを使用します)。

"use strict";

var foo = {};

foo.bar = "moon";
console.log(foo.bar); // "moon"

if (foo.hasOwnProperty("bar") {
  console.log(foo.bar);
}

関連項目

ドキュメントのタグと貢献者

 このページの貢献者: YuichiNukiyama
 最終更新者: YuichiNukiyama,