About the keyword in js, the most commonly used is... In loops can be used to traverse arrays and objects. in addition to loops, in can also be used to judge.
Determines whether an array or object has a key name, for example:
2 in ['A', 'B', 'C'] // true
"B" in {"a": 1, "B": 2} // true
Note that in only determines the key name and does not ask the key value. For example:
Var obj1 = {"a": 1, "B": 2 };
Obj. B = undefined;
"B" in obj1; // true
Var obj2 = {"a": 1, "B": 2 };
Obj. B = null;
"B" in obj2; // true
Var arr = ['A', 'B', 'C'];
Arr [2] = undefined;
2 in arr; // true
In the preceding cases, the in clause only determines the key name regardless of the key value. There is another situation:
Var obj = {"a": 1, "B": 2 };
Delete obj. B;
"B" in obj; // false
Delete deletes the attributes of the object, so that in returns false.
In can only be used to judge arrays and objects. It can only return boolean values but not other types. Therefore, it is useful in some judgments.
Usage of for in js
For (var I = 0; I <len; I ++) can be replaced by for in.
For example:
Var a = ["a", "B", "c"];
For (var el in ){
Alert (a [el]);
}
This is to cite all the elements in a. Of course, the above example can be used.
For (var I = 0, len = a. length; I <len; I ++ ){
Alert (a [I]);
}
This method is cyclically listed, but sometimes this method does not necessarily work.
For example:
Var a = {"first": 1, "second": 2, "third": 3 };
At this time, we can only use for in to make effort.
Whether an object can be used for in is exhaustive. We can use the propertyIsEnumerable attribute to determine whether an object can be used for in, which is described as follows:
PropertyIsEnumerable attributes
Returns a Boolean value indicating whether the specified attribute is a part of an object and whether the attribute is enumerable.
Object. propertyIsEnumerable (proName)
Parameters
Object
Required. An object.
ProName
Required. The string value of an attribute name.
Description
If the proName exists in the object and you can use a... If the In loop is exhausted, the propertyIsEnumerable attribute returns true. If the object does not have a specified attribute or the specified attribute is not enumerable, the propertyIsEnumerable attribute returns false. Typically, predefined attributes are not configurable, and user-defined attributes are always configurable.
The propertyIsEnumerable attribute does not consider objects in the prototype chain.