以下是页面打开测试文件的结果
randomColor_foshan_2(){...}函数存在缺陷
1. randomColor_foshan_2(){...}函数
/****
* name:我佛山人
* qq:52739105
* blog:
*/
function randomColor_foshan_2(){
return (new Date().getTime() & 0xffffff).toString(16);
}
2. 测试缺陷
var date = new Date();
date: Sun Jun 06 2010 15:41:09 GMT+0800 (China Standard Time)
date.getTime(): 1275810069957
date = date.getTime().toString(2);
date: 10010100100001100001101001011110111000101
date = date.substring(0, date.length-24)+"000000000000000000000000";//24个0
date: 10010100100001100000000000000000000000000
date = parseInt(date,2);
date: 1275806613504
date = new Date(date);
date: Sun Jun 06 2010 14:43:33 GMT+0800 (China Standard Time)
date.getTime(): 1275806613504
(date.getTime()&0xffffff): 0
(date.getTime()&0xffffff).toString(16): 0
3. 测试结论
从以上的测试中可以看到:
(1) randomColor_foshan_2(){...}函数存在返回值为"0"(不是6个代表颜色的字符)的可能性。
(2) randomColor_foshan_2(){...}函数返回"0"的可能性很小,每隔 0xffffff + 1 = 16777216 ms (大约 4.66小时) 发生一次, 。
以下是测试文件的源代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>randomColor_foshan_2_bugTest</title>
<style type="text/css">
pre{
border: 1px dashed #ccc;
}
p strong {
background: #303030;
color: #fff;
font-weight: normal;
display: block;
padding: 10px 0;
}
p u {
background: #f99;
color: #000;
font-weight: normal;
display: block;
padding: 3px 0;
text-decoration: none;
}
</style>
</head>
<body>
<h1>randomColor_foshan_2(){...}函数存在缺陷</h1>
<h3>1. randomColor_foshan_2(){...}函数</h3>
<div>
<pre>
/****
* name:我佛山人
* qq:52739105
* blog:
*/
function randomColor_foshan_2(){
return (new Date().getTime() & 0xffffff).toString(16);
}
</pre>
</div>
<h3>2. 测试缺陷</h3>
<div id="debug"></div>
<h3>3. 测试结论</h3>
<div>
<p>从以上的测试中可以看到:</p>
<p>(1) randomColor_foshan_2(){...}函数存在返回值为"0"(不是6个代表颜色的字符)的可能性。</p>
<p>(2) randomColor_foshan_2(){...}函数返回"0"的可能性很小,每隔 0xffffff + 1 = 16777216 ms (大约 4.66小时) 发生一次, 。</p>
</div>
<script type="text/javascript">
//
function debug(s){
document.getElementById("debug").innerHTML += "<p>" + s + "</p>";
}
/****
* name:我佛山人
* qq:52739105
* blog:
*/
function randomColor_foshan_2(){
return (new Date().getTime() & 0xffffff).toString(16);
}
window.onload = function(){
var date = new Date();
debug("<strong>var date = new Date();</strong>");
debug("date: " + date);
debug("date.getTime(): " + date.getTime());
//
date = date.getTime().toString(2);
debug("<strong>date = date.getTime().toString(2);</strong>");
debug("date: " + date);
//
date = date.substring(0, date.length - 24) + "000000000000000000000000";//24个0
debug('<strong>date = date.substring(0, date.length-24)+"000000000000000000000000";//24个0</strong>');
debug("date: " + date);
//
date = parseInt(date, 2);
debug("<strong>date = parseInt(date,2);</strong>");
debug("date: " + date);
//
date = new Date(date);
debug("<strong>date = new Date(date);");
debug("date: " + date);
//
debug("date.getTime(): " + date.getTime());
debug("(date.getTime()&0xffffff): " + (date.getTime() & 0xffffff));
debug("<u>(date.getTime()&0xffffff).toString(16): " + (date.getTime() & 0xffffff).toString(16) + "</u>");
//
}
</script>
</body>
</html>
欢迎各位javascript爱好者亲自测试和提出质疑。
如果你的测试结果与我的不一样,请及时写邮件至:FuDesign2008@163.com 。