The in operator of JavaScript can be used to determine whether an attribute belongs to an object, or it can be used to variable an object's properties
1. Judging attributes belong to object
var mycar = {make: "Honda", Model: "Accord", year:1998}; // Note that the property name must be in the form of a string because make is not a variable if inch Mycar) { document.write (' true ');} Else { document.write (' false '); // Display True
2. For in Traversal object properties
var mycar = {make: "Honda", Model: "Accord", year:1998}; for (var in mycar) { + ': ' + make[p]);}
3. In can be used to judge
if (foo = = ' Bar ' | | foo = = ' Foobar ' | | foo = = ' foo ' ) {//... }// can be written as:if in {' Bar ': ', ' foobar ': ', ' foo ': '} ' ) {// ...}
4. The delete operator can delete properties, mate with in
var mycar = {make: "Honda", Model: "Accord", year:1998}; Delete Mycar.make; in Mycar; // returns false var New Array ("Redwood", "Bay", "Cedar", "oak", "maple"); Delete trees[3]; inch // returns false
5. Try not to iterate through the array with for
The efficiency of collection traversal is: hash > for (;;) > for (in)
Sets the preferred object for the associated operation, followed by a for loop traversal.
Reference: http://www.linuxfly.org/post/524/
JavaScript in operator