1

I have an array of arrays (a multi-dimensional array so to speak, doors in this example), in which wait and point are undefined.

var wait;
var point;
var doors = [
    [wait, doorWrap, 'regY', point, 10, 'bounceOut', 150, 5, 'bounceIn']
// ,[etc.]
];

I want to loop the doors array and for every iteration, execute the key() function, with the doors entries as arguments.

multiKey(doors, 500, 600);

function multiKey (keys, point, wait) {
    for (var i = 0; i < keys.length; i++) {
            wait *= i;
            key.apply(this, keys[i]);
    }
}

Having passed 500 and 600 into the multiKey() function, I expected that point and wait would be defined before the key() function would run -- but heck, point and wait turn out to be undefined.

What is wrong? How could I go about solving this?

Sorry for the title too. I hope the question is clear enough though, because I had a hard time putting my problem into words! Thanks.

4

2 回答 2

1

签名wait中的和数组中的是不同的值。multiKey()waitdoors

您可以将要传递的值分配给循环中的每个数组。

var doors = [
// --v--wait            --v--point
    [0, doorWrap, 'regY', 0, 10, 'bounceOut', 150, 5, 'bounceIn']
// ,[etc.]
];

multiKey(doors, 500, 600);

function multiKey (keys, point, wait) {
    for (var i = 0; i < keys.length; i++) {
            keys[i][0] = wait * i;
            keys[i][3] = point
            key.apply(this, keys[i]);
    }
}

JavaScript 没有指针,它的原始类型在分配时总是被复制。因此,当您包含并在其中时,您将复制undefienddoors数组中。waitpoint

然后,当您将初始值传递给multiKey函数时,它们被分配给完全不同的waitand参数。point然后您将wait参数乘以i,但同样,这与数组中的值完全不同,因此您仍然只是undefined从数组中传递。

于 2013-06-28T00:07:09.423 回答
0

point并且wait声明但未定义。

我看没有对point.

于 2013-06-28T00:03:40.557 回答