You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript函数参数疑问:为何传入plusDivs的1/-1在函数内叫n?

Hey there! The technical term describing this behavior is function parameters and arguments—let me break this down clearly using your code example.

Key Definitions

First, let's get the terminology straight:

  • Argument: The concrete value you pass to a function when calling it. In your button code, -1 and 1 are arguments.
  • Parameter: The variable name defined in a function's declaration that acts as a placeholder for the argument being passed. In function plusDivs(n), n is the parameter—it's what the function uses internally to refer to the value you passed in.
How Your Code Works Step-by-Step

Let's walk through the flow using your actual code:

1. Button Click Triggers Function Call with Arguments

Your HTML buttons use the onclick attribute to call plusDivs() with specific arguments:

<!-- Left button passes -1 as an argument -->
<button style="border:none; font-size: 22px;background-color:black; color:white; position:fixed; top:40%; left:5%" class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">&#10094;</button>

<!-- Right button passes 1 as an argument -->
<button id="rightbutton" style="border:none; font-size: 22px;background-color:black; color:white;position:fixed; top:40%; right:2%" class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">&#10095;</button>

When you click the left button, you're telling the browser: "Run the plusDivs function, and give it the value -1 to work with." Same for the right button with 1.

2. The plusDivs Function Receives the Parameter

The plusDivs function is defined with a parameter n, which catches the argument you passed:

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  // Here, `n` is equal to the argument passed (either -1 or 1)
  showDivs(slideIndex += n);
}

So if you clicked the left button, n becomes -1, and slideIndex is updated to slideIndex + (-1) (subtracts 1). If you clicked the right button, n is 1, so slideIndex increases by 1.

3. The showDivs Function Uses Its Own Parameter

Next, plusDivs calls showDivs() with the updated slideIndex value. Notice showDivs also has a parameter n—this is a separate variable, scoped only to the showDivs function:

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  // Loop through slides to hide all
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  // Wrap around if slide index is out of bounds
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  // Show the current slide
  x[slideIndex - 1].style.display = "block";
}

Here, n is the updated slideIndex value from plusDivs. The function checks if n is outside the range of available slides, adjusts slideIndex to wrap around if needed, then displays the correct slide.

To put it simply: the buttons pass specific values (arguments) to plusDivs, which uses the parameter n to refer to those values internally, then passes an updated value along to showDivs to handle the slide switching.

内容的提问来源于stack exchange,提问作者user2248064

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:43:53