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.

 

概述

如果指定的属性存在于指定的对象中,则 in 运算符会返回 true

语法

prop in objectName 

参数

prop
一个字符串类型或者symbol类型的属性名,或者数组索引。
objectName
需要检测的对象。

描述

下面的例子演示了一些 in 运算符的用法。

// 数组
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是一个数组属性)

// 内置对象
"PI" in Math          // 返回true

// 自定义对象
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar  // 返回true
"model" in mycar // 返回true

in右操作数必须是一个对象值。比如,可以是一个String包装对象,但不能是一个字符串原始值。

var color1 = new String("green");
"length" in color1 // 返回true
var color2 = "coral";
"length" in color2 // 报错(color2不是对象)

使用delete运算符和将属性赋值为undefined

如果你使用 delete 运算符删除了一个属性,则 in 运算符对所删除属性返回 false

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

如果你只是将一个属性的值赋值为 undefined,而没有用 delete 删除它,则 in 运算仍然会返回true

var mycar = {make: "Honda", model: "Accord", year: 1998};
mycar.make = undefined;
"make" in mycar;  // 返回true
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // 返回true

继承属性

如果一个属性是从原型链上继承来的,in 运算符也会返回 true

"toString" in {}; // 返回true

规范

规范 状态 备注
ECMAScript 2017 Draft (ECMA-262)
Relational Operators
Draft  
ECMAScript 2015 (6th Edition, ECMA-262)
Relational Operators
Standard  
ECMAScript 5.1 (ECMA-262)
The in Operator
Standard  
ECMAScript 3rd Edition (ECMA-262)
The in Operator
Standard Initial definition. Implemented in JavaScript 1.4.


浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

 

相关链接

文档标签和贡献者

 此页面的贡献者: zachary05, AlexChao, SphinxKnight, teoli, ziyunfei
 最后编辑者: zachary05,