Wednesday, April 15, 2020

Javascript: 動態函數變量

通過arguments來取得變量,即使函數沒有聲明,也可以由序列ID獲得,很有趣…

var images = new Array()
function preload() {
for (i = 0; i < preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i]
}
}
preload(
"http://domain.tld/gallery/image-001.jpg",
"http://domain.tld/gallery/image-002.jpg",
"http://domain.tld/gallery/image-003.jpg"
)

Reference: https://perishablepress.com/3-ways-preload-images-css-javascript-ajax/

No comments: