First, Judge
Grammar
Prop in ObjectName
The In operator returns true if the object that the objectname points to contains prop this property or a key value.
Copy Code code as follows:
var arr = [' One ', ' two ', ' three ', ' four '];
arr.five = ' 5 ';
0 in Arr;//true
' One ' in arr; False, only the key value of the array can be judged
' Five ' in Arr;//true, ' Five ' is a property of the Arr object
' Length ' in arr;//true
Prototype chain
The in operator queries the given prop property on the entire prototype chain
Copy Code code as follows:
Object.prototype.sayHello = ' Hello,world ';
var foo = new Object ();
' SayHello ' in foo;//true;
' ToString ' in foo;//true;
' hasOwnProperty ' in foo;//true;
Objects and literal quantities
The in operator appears to be different when dealing with objects and literals of certain types (String,number)
Copy Code code as follows:
var sayhelloobj = new String (' Hello,world ');
var SayHello = ' Hello,world ';
var numobj = new number (1);
var num = 1;
' ToString ' in sayhelloobj; True
' ToString ' in SayHello; Type error
' ToString ' in Numobj;//true
' ToString ' in num;//type error
The reason for this is that the introduction of string objects and literal conversions in MDN seems to explain this reason:
Because JavaScript automatically converts between string primitives and string objects, you can call any of the methods of The string object on a string primitive. JavaScript automatically converts the string primitive to a temporary string object, calls the method, then discards the T Emporary String object. For example, your can use the String.Length property on a string primitive created from a string literal
Try to understand that because in is an operator rather than a method, it is not possible to have the string literal automatically converted to a string object, and because the in operator is not an object but rather a string (according to Douglas, Just the type of object-like), so the report type is wrong.
Second, traverse
A commonly used for...in loop statement in which the in-need follows another set of syntax specifications:
For (variable in object)
Statement
Unlike using in as an operator alone, the for...in loop statement traverses only user-defined properties, including custom attributes on the prototype chain, without traversing the built-in (build-in) properties such as ToString.
Object
Copy Code code as follows:
function Bird () {
This.wings = 2;
This.feet = 4;
This.flyable = true;
}
var chicken = new Bird ();
Chicken.flyable = false;
For (var p in chicken) {
Alert (' chicken. ' + p + ' = ' + chicken[p]);
}
String object, tested Firefox,chrome,opera,safari browser is given the result of the annotation, only IE browser gives ' more ' and ' world '
Copy Code code as follows:
var str = new String (' Hello ');
Str.more = ' world ';
For (var p in str) {
Alert (P);//' More ', 0,1,2,3,4
Alert (str[p]);//' World ', ' h ', ' e ', ' l ', ' l ', ' O '
}
Literal amount
Key values and properties that traverse the literal value of an array
Copy Code code as follows:
var arr = [' One ', ' two ', ' three ', ' four '];
arr.five = ' five ';
For (var p in arr) {
Alert (arr[p]);//' One ', ' two ', ' three ', ' four ', ' five '
}
Traverses string literals, although using the in operator before a string literal is reported to be a type error, but the following code works correctly, IE is silent
Copy Code code as follows:
var str = ' Hello ';
Str.more = ' world ';
For (var p in str) {
Alert (p);//0,1,2,3,4
Alert (str[p]);//' H ', ' e ', ' l ', ' l ', ' O '
}
In a comprehensive
ECMA Although there are specifications, but there are differences between browsers, in view of this, it is not recommended to use for...in to traverse the string, it is not recommended to go through the array (as shown in the example, the array plus the custom attributes, the traversal will be messed up)
In terms of traversing objects, we can also use the object's built-in method hasOwnProperty () to eliminate the attributes on the prototype chain, further accelerate the traversal speed, and improve performance
Copy Code code as follows:
function each (object, callback, args) {
VAR prop;
For (prop in object) {
if (Object.hasownproperty (i)) {
Callback.apply (prop, args);
}
}
}