aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/in/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/in/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/operators/in/index.html145
1 files changed, 145 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/in/index.html b/files/zh-cn/web/javascript/reference/operators/in/index.html
new file mode 100644
index 0000000000..acd8d18255
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/operators/in/index.html
@@ -0,0 +1,145 @@
+---
+title: in
+slug: Web/JavaScript/Reference/Operators/in
+tags:
+ - JavaScript
+ - Operator
+ - Relational Operators
+translation_of: Web/JavaScript/Reference/Operators/in
+---
+<p>{{jsSidebar("Operators")}}</p>
+
+<p>如果指定的属性在指定的对象或其原型链中,则<strong><code>in</code> 运算符</strong>返回<code>true</code>。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/expressions-inoperator.html")}}</div>
+
+
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox notranslate"><em>prop</em> in <em>object</em></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>prop</code></dt>
+ <dd>一个字符串类型或者 symbol 类型的属性名或者数组索引(非symbol类型将会强制转为字符串)。</dd>
+</dl>
+
+<dl>
+ <dt><code>objectName</code></dt>
+ <dd>检查它(或其原型链)是否包含具有指定名称的属性的对象。</dd>
+</dl>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>下面的例子演示了一些 <code>in</code> 运算符的用法。</p>
+
+<pre class="brush:js notranslate">// 数组
+var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
+0 in trees // 返回true
+3 in trees // 返回true
+6 in trees // 返回false
+"bay" in trees // 返回false (必须使用索引号,而不是数组元素的值)
+
+"length" in trees // 返回true (length是一个数组属性)
+
+Symbol.iterator in trees // 返回true (数组可迭代,只在ES2015+上有效)
+
+
+// 内置对象
+"PI" in Math // 返回true
+
+// 自定义对象
+var mycar = {make: "Honda", model: "Accord", year: 1998};
+"make" in mycar // 返回true
+"model" in mycar // 返回true
+</pre>
+
+<p><code>in</code>右操作数必须是一个对象值。例如,你可以指定使用<code>String</code>构造函数创建的字符串,但不能指定字符串文字。</p>
+
+<pre class="brush:js notranslate">var color1 = new String("green");
+"length" in color1 // 返回true
+var color2 = "coral";
+"length" in color2 // 报错(color2不是对象)
+</pre>
+
+<h3 id="Using_in_with_deleted_or_undefined_properties" name="Using_in_with_deleted_or_undefined_properties">对被删除或值为 undefined 的属性使用<code>in</code></h3>
+
+<p>如果你使用 <code><a href="/zh-CN/docs/JavaScript/Reference/Operators/delete" title="zh-CN/docs/JavaScript/Reference/Operators/Special/delete">delete</a></code> 运算符删除了一个属性,则 <code>in</code> 运算符对所删除属性返回 <code>false</code>。</p>
+
+<pre class="brush:js notranslate">var mycar = {make: "Honda", model: "Accord", year: 1998};
+delete mycar.make;
+"make" in mycar; // 返回false
+
+var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
+delete trees[3];
+3 in trees; // 返回false
+</pre>
+
+<p>如果你只是将一个属性的值赋值为{{jsxref("Global_Objects/undefined", "undefined")}},而没有删除它,则 <code>in</code> 运算仍然会返回<code>true</code>。</p>
+
+<pre class="brush:js notranslate">var mycar = {make: "Honda", model: "Accord", year: 1998};
+mycar.make = undefined;
+"make" in mycar; // 返回true
+</pre>
+
+<pre class="brush:js notranslate">var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
+trees[3] = undefined;
+3 in trees; // 返回true
+</pre>
+
+<h3 id="Inherited_properties" name="Inherited_properties">继承属性</h3>
+
+<p>如果一个属性是从原型链上继承来的,<code>in</code> 运算符也会返回 <code>true</code>。</p>
+
+<pre class="brush:js notranslate">"toString" in {}; // 返回true
+</pre>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-relational-operators', 'Relational Operators')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-11.8.7', 'The in Operator')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES3', '#sec-11.8.7', 'The in Operator')}}</td>
+ <td>{{Spec2('ES3')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.4.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.operators.in")}}</p>
+
+<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>
+
+<ul>
+ <li><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a></code></li>
+ <li><code><a href="/zh-CN/docs/JavaScript/Reference/Operators/delete" title="zh-CN/docs/JavaScript/Reference/Operators/Special/delete">delete</a></code></li>
+ <li>{{jsxref("Object.prototype.hasOwnProperty()")}}</li>
+ <li>{{jsxref("Reflect.has()")}}</li>
+ <li><a href="/zh-CN/docs/Enumerability_and_ownership_of_properties" title="/zh-CN/docs/Enumerability_and_ownership_of_properties">属性的可枚举性和所有权</a></li>
+</ul>