<html>
<head>
<style type="text/css">
#mycanvas{
width:500px;
height:450px;
border:2px solid green;
font-size:30px;
}
</style>
<script>
window.onload = function(){
vcan = {};
vcan.main = {}
vcan.main.repNode = [];
vcan.main.currentTransform = {'id':0, 'name':'myobj'}
document.getElementById("mycanvas").addEventListener('mousemove', mymousemove);
function mymousemove(){
var currAdTime = new Date().getTime();
console.log(currAdTime);
var myObj = extend(vcan.main.currentTransform , {mdTime:currAdTime, func:'add'})
vcan.main.repNode.push(myObj);
}
function extend (fobj, sobj){
if((typeof fobj == 'object') && (typeof sobj == 'object')){
for(var prop in sobj){
fobj[prop] = sobj[prop];
}
return fobj;
}else{
alert("it seems that the arguments you passed are not object");
}
}
document.getElementById("test").onclick = function(){
var output = "";
for(var i=0; i< vcan.main.repNode.length; i++){
output += vcan.main.repNode[i].mdTime;
output += "<br />";
}
document.getElementById("result").innerHTML = output;
}
}
</script>
</head>
<body>
<div id="mycanvas">
Please move the mouse inside this box
And click on Show Result
</div>
<button id="test">
Show Result
</button>
<div id="result">
</div>
</body>
</html>
爲了解決問題,請將上述代碼渲染到瀏覽器中。點擊Show Result按鈕後,會顯示時間。我的問題是,爲什麼所有的同時顯示。爲什麼最新值覆蓋到JavaScript中的對象數組?
時間的最新值覆蓋對象數組的不同元素。
同時,如果我們看到控制檯,那麼時間會有所不同。
經過漫長的搜索,我無法解決這個問題。請大家幫我解決這個問題。
擺弄:[http://jsfiddle.net/WacV4/](http://jsfiddle.net/WacV4/) – RobMasters