随机函数与JSON

1.随机函数

//随机函数
//Math.round() 四舍五入
Math.round(Math.random());//区0-1之间的整数即0或1
//0~10之间的整数
Math.round( Math.random()*10);
//5~10之间的整数
Math.round( Math.random()*5+5);
//20~100之间的整数
Math.round( Math.random()*80+20);
//x~y之间的整数
Math.round( Math.random()*(y-x)+x);
//0~x之间的整数
Math.round( Math.random()*x);
//1~x
Math.ceil( Math.random()*x);

2.JSON

var json={"name":"leo","age":24};
json.name;//leo
json["nume"];//leo

var imgData={
	imgUrl:["img/1.png","img/2.png","img/3.png","img/4.png"],
	txt:["图片一","图片二","图片三","图片四"]
}
imgData.imgUrl[0];//img/1.png

//json遍历
var json1={'name':'carol','age':18,'fun':'moile'};
for (var attr1 in json1) {
//	alert(attr1);
	alert(json1[attr1]);
}

  

原文地址:https://www.cnblogs.com/yangxue72/p/7975819.html