From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/object/is/index.html | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/object/is/index.html (limited to 'files/ja/web/javascript/reference/global_objects/object/is') diff --git a/files/ja/web/javascript/reference/global_objects/object/is/index.html b/files/ja/web/javascript/reference/global_objects/object/is/index.html new file mode 100644 index 0000000000..ab871c5ab4 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/object/is/index.html @@ -0,0 +1,125 @@ +--- +title: Object.is() +slug: Web/JavaScript/Reference/Global_Objects/Object/is +tags: + - Comparison + - Condition + - Conditional + - ECMAScript 2015 + - Equality + - JavaScript + - Method + - Object +translation_of: Web/JavaScript/Reference/Global_Objects/Object/is +--- +
{{JSRef}}
+ +

Object.is() メソッドは 2 つの値が同一値であるかどうかを判定します。

+ +

構文

+ +
Object.is(value1, value2);
+ +

引数

+ +
+
value1
+
比較する 1 つ目の値。
+
value2
+
比較する 2 つ目の値。
+
+ +

返値

+ +

{{jsxref("Boolean")}} で、 2 つの引数が同一値であるかどうかを表します。

+ +

解説

+ +

Object.is() は 2 つの値が同一値であるかどうかを判定します。2 つの値が以下の規則の一つに当てはまる場合に同一となります。

+ + + +

このメソッドは {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} 演算子による等値比較と同じものではありません== 演算子は等値性比較の前に (同じ型でなければ) 両辺に対して様々な型変換を適用します (結果、例えば "" == falsetrue に評価されます) が、Object.is は両辺どちらの値にも型変換を行いません。

+ +

また {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} 演算子による同値比較とも同じものでもありません=== 演算子は (そして == 演算子も) 数値 -0+0 は同じものとして扱い、 {{jsxref("Number.NaN")}} と {{jsxref("NaN")}} は異なるものとして扱います。

+ +

+ +

Object.is の使用

+ +
Object.is('foo', 'foo');     // true
+Object.is(window, window);   // true
+
+Object.is('foo', 'bar');     // false
+Object.is([], []);           // false
+
+var foo = { a: 1 };
+var bar = { a: 1 };
+Object.is(foo, foo);         // true
+Object.is(foo, bar);         // false
+
+Object.is(null, null);       // true
+
+// 特殊なケース
+Object.is(0, -0);            // false
+Object.is(-0, -0);           // true
+Object.is(NaN, 0/0);         // true
+
+ +

ポリフィル

+ +
if (!Object.is) {
+  Object.defineProperty(Object, "is", {
+    value: function (x, y) {
+      // 同値アルゴリズム
+      if (x === y) { // ステップ 1 から 5、および 7 から 10
+        // ステップ 6.b から 6.e までの場合: +0 != -0
+        return x !== 0 || 1 / x === 1 / y;
+      } else {
+        // ステップ 6.a の場合: NaN == NaN
+        return x !== x && y !== y;
+      }
+    }
+  });
+}
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-object.is', 'Object.is')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.Object.is")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf