A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty below:

Object.prototype.hasOwnProperty() - JavaScript | MDN

Object.prototype.hasOwnProperty()

Baseline Widely available

hasOwnProperty() は Object インスタンスのメソッドで、オブジェクト自身が(継承されていない)指定されたプロパティを持っているかどうかを示す論理値を返します。

メモ: Object.hasOwn() は hasOwnProperty() よりもブラウザーの対応状況の面で推奨されます。

試してみましょう
const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty("property1"));
// Expected output: true

console.log(object1.hasOwnProperty("toString"));
// Expected output: false

console.log(object1.hasOwnProperty("hasOwnProperty"));
// Expected output: false
構文 引数
prop

テストするプロパティの名前の文字列またはシンボル。

返値

オブジェクトが指定したプロパティを自分自身で保有している場合は true を返し、そうでない場合は false を返します。

解説

hasOwnProperty() メソッドは、指定したプロパティがオブジェクトの直接のプロパティである場合 (たとえその値が null または undefined であっても) 、 true を返します。プロパティが継承されているか、まったく宣言されていない場合は false を返します。 in 演算子とは異なり、このメソッドはオブジェクトのプロトタイプチェーンに指定したプロパティがあるかどうかを調べません。

なぜなら、ほとんどのオブジェクトは Object の子孫であり、そのメソッドを継承しているからです。例えば配列 (Array) はオブジェクト Object なので、インデックスが存在するかどうかを調べるには hasOwnProperty() メソッドを使用することができます。

const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined

このメソッドは、再実装されたオブジェクトや、 null プロトタイプオブジェクト(Object.prototype を継承していない)では利用できません。これらの場合の例は下記の通りです。

例 hasOwnProperty を使ってプロパティの存在を調べる

オブジェクト o が prop という名前のプロパティを持っているかどうかを特定する例を以下に示します。

const example = {};
example.hasOwnProperty("prop"); // false

example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop' が定義されている

example.prop = null;
example.hasOwnProperty("prop"); // true - null 値を持つ独自プロパティ

example.prop = undefined;
example.hasOwnProperty("prop"); // true - undefined 値を持つ独自プロパティ
直接のプロパティと継承されたプロパティ

以下の例では、直接のプロパティとプロトタイプチェーンを通じて継承されたプロパティを区別します。

const example = {};
example.prop = "exists";

// `hasOwnProperty` は直接のプロパティについてのみ true を返す
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false

// 演算子 `in` は、直接または継承されたプロパティに対して true を返す
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true
オブジェクトのプロパティの反復処理

以下の例では、継承されたプロパティを除いてオブジェクトのプロパティを反復処理する方法を示します。

const buz = {
  fog: "stack",
};

for (const name in buz) {
  if (buz.hasOwnProperty(name)) {
    console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`);
  } else {
    console.log(name); // toString or something else
  }
}

なお、 for...in ループですでに列挙可能なアイテムのみが反復処理されるので、 hasOwnProperty 自体は列挙可能なアイテムに厳密に限定されているため、ループ内に列挙できないプロパティが見られないことに基づいて想定するべきではありません (Object.getOwnPropertyNames() のように)。

プロパティ名としての hasOwnProperty の使用

JavaScript は hasOwnProperty というプロパティ名を保護していません。この名前を持ったプロパティを持つオブジェクトでは、正しくない結果が返る可能性があります。

const foo = {
  hasOwnProperty() {
    return false;
  },
  bar: "Here be dragons",
};

foo.hasOwnProperty("bar"); // 再実装では常に false を返す

この問題を克服するために推奨される方法は、代わりに Object.hasOwn() を使用することです(対応しているブラウザーで)。他にも、外部の hasOwnProperty を使用する方法があります。

const foo = { bar: "Here be dragons" };

// Use Object.hasOwn() method - recommended
Object.hasOwn(foo, "bar"); // true

// Use the hasOwnProperty property from the Object prototype
Object.prototype.hasOwnProperty.call(foo, "bar"); // true

// 別な Object の hasOwnProperty 使用して、
// this を foo に設定して呼び出す
({}).hasOwnProperty.call(foo, "bar"); // true

なお、後者の場合は新しくオブジェクトを生成しません。

Object.create(null) で作成されたオブジェクト

null プロトタイプオブジェクトは Object.prototype を継承していないので、 hasOwnProperty() はアクセス不可になります。

const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty は関数ではない

この場合の解決策は前の節と同じです。構成については Object.hasOwn() を使用し、そうでなければ外部オブジェクトの hasOwnProperty() を使用してください。

仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4