王新阳

wangxinyang

JS仿PHP shuffle、range、in_array

Array.prototype.shuffle = function() {
	for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
	return this;
};

function range(startNum, endNum, stepNum){
	for(var arr=[], i=startNum; i<=endNum; arr.push(i), i+=(stepNum ? stepNum : 1));
	return arr;
}

function in_array(obj, arr){
	for(var i in arr){
		if(arr[i]===obj)return true;
	}
	return false;
}

在JavaScript 1.6中in_array有对应的原生函数arr.indexOf(search)
注:IE浏览器暂不支持 JavaScript 1.6

2015-06-13
2024-11-23 星期六 农历十月二十三