简体   繁体   English

Javascript,这个函数中的 (n) 和方括号是做什么的?

[英]Javascript, what are (n) and the square brackets doing in this function?

Just got a couple of questions to help me understand this script, as where it is OP, the explanation is pretty thin.只是有几个问题可以帮助我理解这个脚本,因为它是 OP,解释很简单。

1) (n) is passed into the functions plusDivs and showDivs. 1) (n)传递给函数 plusDivs 和 showDivs。 How does this (n) work?这个(n)如何工作的? Is it just an empty container to store the data in the function.是否只是一个空容器来存储函数中的数据。

2) With this line x[i].style.display="none"; 2) 用这一行x[i].style.display="none"; and x[slideIndex-1].style.display="block";x[slideIndex-1].style.display="block"; with the square brackets used, is the code inside the square brackets executed first?使用方括号后,方括号内的代码是先执行的吗? What are the square brackets doing or called in this instance.在这种情况下,方括号在做什么或调用什么。

Although functions are explained in online tutorials in a basic way, some of this information I'm asking is quite hard to find out so would appreciate some help .尽管在线教程中以基本方式解释了函数,但我要求的某些信息很难找到,因此希望得到一些帮助。

Thanks Emma.谢谢艾玛。

JS: JS:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
    showDivs(slideIndex += n);
}

function showDivs(n) {
    var i;
    var x = document.getElementsByClassName("mySlides");
    if (n > x.length) { slideIndex = 1 } 
    if (n < 1) { slideIndex = x.length };
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none"; 
    }
    x[slideIndex-1].style.display = "block"; 
}

HTML: HTML:

<button style="border:none; font-size: 22px;background-color:black; color:white; position:fixed; top:30%; left:5%" onclick="plusDivs(-1)">&#10094;</button>
<button id="rightbutton" style="border:none; font-size: 22px;background-color:black; color:white;position:fixed; top:30%; right:5%" onclick="plusDivs(1)">&#10095;</button>

In JavaScript, functions are portions of code that take caller-supplied information called "arguments" and use them in the code.在 JavaScript 中,函数是代码的一部分,它接受调用者提供的称为“参数”的信息,并在代码中使用它们。 The syntax of a function is函数的语法是

function (x, y) {
    return x + y;
}

If the above function were to be called:如果要调用上述函数:

(function (x, y) {
    return x + y;
})(1, 1)

The result would be 2 .结果是2

The [ brackets ] are subscript operators. [括号]是下标运算符。 In the context of arrays, they take a numeric index and return the element at that location:在数组的上下文中,它们采用数字索引并返回该位置的元素:

[1, 2, 3][0] === 1

In the context of objects, they take a key and return the mapped value:在对象的上下文中,它们接受一个键并返回映射的值:

(({
     'foo' : 'bar'
})['foo']) === 'bar'

In your case, the function getElementsByClassName returns an array of elements, so the brackets denote array subscription and are used to retrieve an element of the array.在您的情况下,函数getElementsByClassName返回一个元素数组,因此括号表示数组订阅并用于检索数组的元素。

1) (n) is passed into the functions plusDivs and showDivs. 1) (n) 传递给函数 plusDivs 和 showDivs。 How does this (n) work?这个 (n) 是如何工作的? Is it just an empty container to store the data in the function.是否只是一个空容器来存储函数中的数据。

an integer passed into plusDivs() function, its mean by pressing buttons you pass "-1 and +1" integer as argument to your function and in your function it will add/contact it from you current slide index that will cause your slider moves to forward(next slide) or backward(prev slide)传递给 plusDivs() 函数的整数,它的意思是通过按下按钮将“-1 和 +1”整数作为参数传递给函数,并且在您的函数中,它将从您当前的幻灯片索引添加/联系它,这将导致您的滑块移动向前(下一张幻灯片)或向后(上一张幻灯片)

2) With this line x[i].style.display="none"; 2) 用这一行 x[i].style.display="none"; and x[slideIndex-1].style.display="block";和 x[slideIndex-1].style.display="block"; with the square brackets used, is the code inside the square brackets executed first?使用方括号后,方括号内的代码是先执行的吗? What are the square brackets doing or called in this instance.在这种情况下,方括号在做什么或调用什么。

As i mentioned in comments they are for finding nth element inside an array.正如我在评论中提到的,它们用于在数组中查找第 n 个元素。 at first of your code you find all slides and cached them into a variable, so when you need to find nth slide youmost use something like this slide[slide number 2] (this is just pseudo code)在您的代码中,您首先找到所有幻灯片并将它们缓存到一个变量中,因此当您需要找到第 n 张幻灯片时,您最常用的是这张slide[slide number 2] (这只是伪代码)

I don't think the answers in this case are very clear.我认为这种情况下的答案不是很清楚。 The (n) and i in your question are just free variables.您问题中的(n)i只是自由变量。 They could be any letter (eg i could be 'b' or some other letter).它们可以是任何字母(例如, i可以是'b'或其他一些字母)。

They are just naming and referencing the var properties.它们只是命名和引用 var 属性。

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

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