Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:保存一个变量';通话间隔的价值是多少?_Javascript - Fatal编程技术网

javascript:保存一个变量';通话间隔的价值是多少?

javascript:保存一个变量';通话间隔的价值是多少?,javascript,Javascript,我有一些代码如下所示: createEntity = function (imageSource, baseX, baseY) { tmpIndex = images.length; images[tmpIndex] = new Image(); entities[tmpIndex] = new Entity; images[tmpIndex].onload = function () { entities[tmpIndex].ready = tr

我有一些代码如下所示:

createEntity = function (imageSource, baseX, baseY) {
    tmpIndex = images.length;
    images[tmpIndex] = new Image();
    entities[tmpIndex] = new Entity;
    images[tmpIndex].onload = function () {
        entities[tmpIndex].ready = true;

        // How do I get the line above to use the value of tmpIndex
        //  that existed when it was created?
        // That is to say, I want the images[1].onload function to
        //  change entities[1].ready, not entities[4].ready
        //  (assuming I created 5 objects sequentially
        //   before any images finished loading)

    }
    images[tmpIndex].src = imageSource;
    entities[tmpIndex].x = baseX;
    entities[tmpIndex].y = baseY;
}

createEntity("images/background.png", 0, 0);
createEntity("images/pawn1.png",0,0);
createEntity("images/pawn2.png",30,30);
createEntity("images/pawn3.png",60,60);
createEntity("images/pawn4.png",90,90);

问题是,当我按顺序加载所有5个图像时,正如上面的代码所示,我的onload函数使用当前值tmpIndex触发,而不是创建函数时存在的值。是否有一种简单的方法可以使实体[somenumber].ready正确切换?

您需要将
tmpIndex
声明为局部变量。做这个改变

tmpIndex = images.length;


您需要将
tmpIndex
声明为局部变量。做这个改变

tmpIndex = images.length;


为什么
tmpIndex
必须在createEntity函数之外可见?如果没有,只需在函数中声明它,如下所示:
var tmpIndex=images.length

即使在
createEntity
函数完成执行后,您的
onload
回调仍将能够读取其值,因为它将保留对创建它的作用域的引用


每个执行范围都不同,因此每次调用
createEntity
时,都会创建不同的范围和不同的onload回调函数,该函数存储对该执行范围的引用,因此能够使用在那里定义的变量。

为什么
tmpIndex
必须在createEntity函数之外可见?如果没有,只需在函数中声明它,如下所示:
var tmpIndex=images.length

即使在
createEntity
函数完成执行后,您的
onload
回调仍将能够读取其值,因为它将保留对创建它的作用域的引用


每个执行范围都是不同的,因此每次调用
createEntity
时,都会创建不同的范围和不同的onload回调函数,该函数存储对该执行范围的引用,因此能够使用其中定义的变量。下面是一个在onload上捕获onerror的JSFIDLE示例:

以下是令人振奋的部分:

//Don't forget to declare a local variable !
var tmpIndex = images.length;

没错。下面是一个在onload上捕获onerror的JSFIDLE示例:

以下是令人振奋的部分:

//Don't forget to declare a local variable !
var tmpIndex = images.length;

将其存储在另一个不更改的变量中不更改值如何?createEntity函数中的局部变量应执行此操作?将其存储在另一个不更改的变量中不更改值如何?createEntity函数中的局部变量应执行此操作,谢谢。我不是世界上最好的程序员,所以有时我无法确定范围。这正是我需要的。很漂亮,谢谢。我不是世界上最好的程序员,所以有时我无法确定范围。这正是我所需要的。我知道你的答案排在第一位,似乎和代码学徒的答案是一样的,但他的答案对我来说更容易理解和实现。他解释了为什么它坏了,以及如何修复它,你问为什么它需要它拥有的范围,如果范围不重要怎么办。。。我的主要问题是我没有考虑范围,如果没有这个背景,你的答案似乎没有那么有意义。我知道你的答案是第一位的,似乎说的和代码学徒的一样,但他的答案对我来说更容易理解和实现。他解释了为什么它坏了,以及如何修复它,你问为什么它需要它拥有的范围,如果范围不重要怎么办。。。我的主要问题是,我没有考虑范围,没有这个背景,你的回答似乎没有那么有意义。