in操做符用來判斷某個屬性屬於某個對象,能夠是對象的直接屬性,也能夠是經過prototype繼承的屬性。(參見hasOwnProperty)數組
n 對於通常的對象屬性須要用字符串指定屬性的名稱prototype
如:
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar // returns true
"model" in mycar // returns true
n 對於數組屬性須要指定數字形式的索引值來表示數組的屬性名稱(固有屬性除外,如length)。對象
// Arrays
var trees = new Array("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)
n in的右邊必須是一個對象,如:你能夠指定一個用String構造器生成的,可是不能指定字符串直接量的形式:繼承
var color1 = new String("green");
"length" in color1 // returns true
var color2 = "coral";
"length" in color2 // generates an error (color is not a String object)
n 若是你使用delete操做符刪除了一個屬性,再次用in檢查時,會返回false,如:索引
var mycar = {make: "Honda", model: "Accord", year: 1998};
delete mycar.make;
"make" in mycar; // returns false
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
3 in trees; // returns false
n 若是你把一個屬性值設爲undefined,可是沒有使用delete操做符,使用in檢查,會返回true.ip
var mycar = {make: "Honda", model: "Accord", year: 1998};
mycar.make = undefined;
"make" in mycar; // returns true
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // returns true