我正在查看一些代码片段,我发现多个元素在一个节点列表上调用一个函数,并将forEach应用于一个空数组.
例如,我有类似的东西:
[].forEach.call( document.querySelectorAll('a'), function(el) {
// whatever with the current node
});
Run Code Online (Sandbox Code Playgroud)
但我无法理解它是如何工作的.任何人都可以解释我在forEach前面的空数组的行为以及它是如何call工作的?
我的问题是:是否可以在不重写相同变量的情况下添加相同的元素.我正在创建一个滑块,我需要将div一个类附加slide-el到块中slider.这是代码的一部分
var body, html, sliderBody, btnLeft, btnRight, i, parts, vHeight, vWidth;
//Variable definitions
var i = 0,
parts = 3,
//Main html elements
body = document.body,
html = document.element,
//viewport Height and Width
vHeight = window.innerHeight,
vWidth = window.innerWidth,
sliderBody = _id("slider"),
btnLeft = _id("btn-left"),
btnRight = _id("btn-right"),
urls = ["http://www.wallpapereast.com/static/images/pier_1080.jpg",
"http://www.wallpapereast.com/static/images/pier_1080.jpg",
"http://www.wallpapereast.com/static/images/pier_1080.jpg",
"http://www.wallpapereast.com/static/images/pier_1080.jpg"];
slide = _createEl("div");
slide.className += "slide-el";
function _id(el){
return document.getElementById(""+ el +"");
}
function _createEl(el){
return document.createElement(""+ el +"");
} …Run Code Online (Sandbox Code Playgroud) javascript ×2
appendchild ×1
arrays ×1
ecmascript-5 ×1
element ×1
foreach ×1
jquery ×1
nodelist ×1