这个问题以前可能已经问过了,但我能找到的都是关于C和Bash等的问题。
基本上,我很难理解函数参数和它们引用的内容。
我知道你通常在调用函数时设置参数,例如doSomething(3,'Hello')等,但当我从教程中阅读代码时,就会这样;
window.onload = initAll;
function initAll() {
if (document.getElementById) {
for (var i=0; i<24; i++) {
setSquare(i);
}
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
var colBasis = colPlace[thisSquare] * 15;
var newNum = colBasis + getNewNum() + 1;
document.getElementById(currSquare).innerHTML = newNum;
}
function getNewNum() {
return Math.floor(Math.random() * 15);
}thisSquare的参数setSquare()是从哪里获取的?
发布于 2012-01-10 06:22:32
在您的第一个函数initAll()中,您将调用setSquare(i)。在本例中,i是参数。根据initAll()的说法,i是for循环中的一个数字。从本质上讲,您正在为每个从0到24的平方数调用setSquare。
setSquare()函数已将i重命名为thisSquare。现在,在setSquare()函数中的任何位置,thisSquare都被设置为与i之前相同的值。
希望这能帮上忙,祝你好运。
发布于 2012-01-10 06:21:40
在initAll内部有以下代码:
for (var i=0; i<24; i++) {
setSquare(i);
}所以initAll调用setSquare 24次。每次都传入i的值。(0、1、2等)。因此,i的值为thisSquare
发布于 2012-01-10 06:23:00
在initAll函数中调用setSquare,该函数向它传递一个从0到23的值。initAll函数在页面加载时调用(理论上)。
https://stackoverflow.com/questions/8795952
复制相似问题