admin管理员组文章数量:1291000
I have a couple of arrays, like so:
var galImgs = ["/gallery/image-001.jpg",
"/gallery/image-002.jpg",
"/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:
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 ma-separated strings. How can I pass the galImgs
array so it will be read as individual arguments?
I have a couple of arrays, like so:
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:
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 ma-separated strings. How can I pass the galImgs
array so it will be read as individual arguments?
-
2
why don't you declare the function with a
galImgs
parameter?function preload(galImgs) { ... }
– Nalum Commented Jul 6, 2011 at 15:18
4 Answers
Reset to default 5However, calling
preload(galImgs);
passes in the array as a single string, instead of ma-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:
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.)
This has the advantage of not using the arguments
pseudo-array, which is a performance hit on most implementations.
If you want, though, you can use an array to pass discrete arguments to preload
, via the apply
function:
preload.apply(undefined, galImgs);
The first argument is used for this
within the function, you can just use undefined
for the default; the second argument is the array, which will show up in the function as discrete arguments.
The problem is your using the arguments keyword to find the passed in event. 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:
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"
You can use gallmgs is an array object and you would have all the accessors of an array in this. Try gallmgs.length.
本文标签: javascriptHow can I pass an array of strings to a function as argumentsStack Overflow
版权声明:本文标题:javascript - How can I pass an array of strings to a function as arguments? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515438a2382856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论