今天在阅读jQuery源码的时候看到在if中使用in操作符,发现原来in不仅仅是用在for中,看来对JavaScript的了解实在太少了:)

对我来说最有用的功能是像PHP中的in_array一样使用,其用途可以类似于PHP中的isset(感谢@littlexiang纠正),如下面的代码所示:

var arr = [1, 2, 3, 4, 5];
var obj = {
			"4": true, 
			"6":false, 
			"9":true, 
			"11":true
		};

if ( 2 in arr) {
	alert('2 in arr');
} else {
	alert('2 NOT in arr');
}

if ( 4 in obj) {
	alert('4 in obj');
} else {
	alert('4 NOT in obj');
}

if ( 6 in obj) {
	alert('6 in obj');
} else {
	alert('6 NOT in obj');
}

if ( 21 in arr) {
	alert('21 in arr');
} else {
	alert('21 NOT in arr');
}

if ( 2 in obj) {
	alert('2 in obj');
} else {
	alert('2 NOT in obj');
}


if ( 0 in arr) {
	alert('0 in arr');
} else {
	alert('0 NOT in arr');
}

if ( 5 in arr) {
	alert('5 in arr');
} else {
	alert('5 NOT in arr');
}

For more information, please refer to: Using the ‘In’ Operator to Iterate Through Arrays and Objects