简体   繁体   English

如何将字符串数组作为 arguments 传递给 function?

[英]How can I pass an array of strings to a function as arguments?

I have a couple of arrays, like so:我有几个 arrays,像这样:

var galImgs = ["http://domain.tld/gallery/image-001.jpg",
               "http://domain.tld/gallery/image-002.jpg",
               "http://domain.tld/gallery/image-003.jpg"],
    preloadImgs = [];

And I'd like to use a simple for loop to load images from the values in the galImgs array, like so:我想使用一个简单for循环从galImgs数组中的值加载图像,如下所示:

function preload() {
    for (i = 0; i < preload.arguments.length; i++) {
    preloadImgs[i] = new Image();
    preloadImgs[i].src = preload.arguments[i];
    }
}

However, calling然而,调用

preload(galImgs);

passes in the array as a single string, instead of comma-separated strings.将数组作为单个字符串传递,而不是逗号分隔的字符串。 How can I pass the galImgs array so it will be read as individual arguments?如何传递galImgs数组,以便将其作为单独的 arguments 读取?

However, calling然而,调用

preload(galImgs);

passes in the array as a single string, instead of comma-separated strings.将数组作为单个字符串传递,而不是逗号分隔的字符串。

No, it passes in the array, not a string.不,它传入数组,而不是字符串。 You've actually gone to too much trouble in your preload function, just use the array you were given:实际上,您在preload function 时遇到了太多麻烦,只需使用给定的数组即可:

function preload(a) {
    for (i = 0; i < a.length; i++) {
        preloadImgs[i] = new Image();
        preloadImgs[i].src = a[i];
    }
}

(And declare the i variable within the function, which I haven't added above; as it is, you're falling prey to The Horror of Implicit Globals .) (并在 function 中声明i变量,我没有在上面添加;事实上,你正在成为隐式全局恐怖的牺牲品。)

This has the advantage of not using the arguments pseudo-array, which is a performance hit on most implementations.这样做的好处是不使用arguments伪数组,这对大多数实现来说都是一个性能问题。

If you want, though, you can use an array to pass discrete arguments to preload , via the apply function:但是,如果您愿意,您可以使用数组通过apply function 将离散的 arguments 传递给preload

preload.apply(undefined, galImgs);

The first argument is used for this within the function, you can just use undefined for the default;第一个参数用于this中,您可以使用undefined作为默认值; the second argument is the array, which will show up in the function as discrete arguments.第二个参数是数组,它将在 function 中显示为离散的 arguments。

The problem is your using the arguments keyword to find the passed in event.问题是您使用 arguments 关键字来查找传入的事件。 What you really want is你真正想要的是

function preload(images) {
    for (i = 0; i < images.length; i++) {
    preloadImgs[i] = new Image();
    preloadImgs[i].src = images[i];
    }
}

To help you understand better, your old way was fine except one thing..为了帮助您更好地理解,您的旧方法很好,除了一件事..

preload.arguments.length == 1
preload.arguments[0] == galImgs
preload.arguments[0][0] == galImgs[0]

You are looking for the apply method:您正在寻找apply方法:

preload.apply(null, galImgs);

Should do it!应该这样做!

There's nothing to worry about having viewing it as a single string.无需担心将其视为单个字符串。 Just by providing index to your array, you would be able to get the individual strings.只需为您的数组提供索引,您就可以获取各个字符串。

galImgs[0] would return you the string "http://domain.tld/gallery/image-001.jpg" galImgs[0] 会返回字符串“http://domain.tld/gallery/image-001.jpg”

You can use gallmgs is an array object and you would have all the accessors of an array in this.您可以使用 gallmgs 是一个数组 object 并且您将在其中拥有一个数组的所有访问器。 Try gallmgs.length.试试galmgs.length。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM