in
operator
返回
true
if the specified property is in the specified object or its prototype chain.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
prop in object
prop
A string or symbol representing a property name or array index (non-symbols will be coerced to strings).
object
prop
).
The following examples show some uses of the
in
operator.
// Arrays
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple']
0 in trees // returns true
3 in trees // returns true
6 in trees // returns false
'bay' in trees // returns false (you must specify the index number, not the value at that index)
'length' in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES2015+)
// Predefined objects
'PI' in Math // returns true
// Custom objects
let mycar = {make: 'Honda', model: 'Accord', year: 1998}
'make' in mycar // returns true
'model' in mycar // returns true
You must specify an object on the right side of the
in
operator. For example, you can specify a string created with the
String
constructor, but you cannot specify a string literal.
let color1 = new String('green')
'length' in color1 // returns true
let color2 = 'coral'
// generates an error (color2 is not a String object)
'length' in color2
in
with deleted or undefined properties
If you delete a property with the
delete
operator, the
in
operator returns
false
for that property.
let mycar = {make: 'Honda', model: 'Accord', year: 1998}
delete mycar.make
'make' in mycar // returns false
let trees = new Array('redwood', 'bay', 'cedar', 'oak', 'maple')
delete trees[3]
3 in trees // returns false
If you set a property to
undefined
but do not delete it, the
in
operator returns true for that property.
let mycar = {make: 'Honda', model: 'Accord', year: 1998}
mycar.make = undefined
'make' in mycar // returns true
let trees = new Array('redwood', 'bay', 'cedar', 'oak', 'maple')
trees[3] = undefined
3 in trees // returns true
in
operator will return
false
for empty array slots. Even if accessing it directly returns
undefined
.
let empties = new Array(3) empties[2] // returns undefined 2 in empties // returns false
To avoid this, make sure a new array is always filled with non-empty values or not write to indexes past the end of array.
let empties = new Array(3).fill(undefined) 2 in empties // returns true
in
operator returns
true
for properties in the prototype chain. (If you want to check for only
non-inherited
properties, use
Object.prototype.hasOwnProperty()
instead.)
'toString' in {} // returns true
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Relational Operators' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
in
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
for...in
delete
Object.prototype.hasOwnProperty()
Reflect.has()