简体   繁体   English

如何将数组/值传递给Javascript函数

[英]How to pass Array/Values to a Javascript function

I am learning Javascript for a project using online resources however i don't know how to get this function working. 我正在使用在线资源为项目学习Javascript,但是我不知道如何使此功能正常工作。

var results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results.length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

Just after the function, i use: checkWinner(results); 就在函数之后,我使用:checkWinner(results);

In a HTML file i use alert to display the variable winner. 在HTML文件中,我使用alert显示变量赢家。 But it obviously doesn't work. 但这显然行不通。 I realise it is a problem with my understanding of scope and global variables. 我了解范围和全局变量是一个问题。

should be 应该

var Results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results[0].length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

checkWinner(Results);

To avoid name collisions name global variables from capital case. 为避免名称冲突,请使用大写字母来命名全局变量。 Also in your code you call the length of the "parent" array. 同样在代码中,您调用“父”数组的长度。 You need to specify the length of the "Child" array 您需要指定“子”数组的长度

你在迭代result[0]阵列(中阵列result[0]但使用的长度result阵列。

You've got to understand the concept of scope . 您必须了解范围的概念。 The variables results and winner are not the same inside and outside the function. 函数内部和外部的变量resultswinner者不相同。

Also, you've got to call the function and return something from it if you want to change the value of the variables outside the function (unless you use globals). 另外,如果要在函数外部更改变量的值,则必须调用该函数并从中返回一些信息(除非使用全局变量)。 This seems to be hard for novice programmers to understand, but merely defining a function doesn't do anything. 对于新手程序员来说,这似乎很难理解,但是仅仅定义一个函数并没有任何作用。

var results =[[a1,a2,a3,a4,a5]];

function checkWinner (results)
{ 
    for (var result in results[0]) 
    {
        if (result > 50)
        {
            return result;
        }
    }
}

var winner = checkWinner(results);

Note that: 注意:

  • I used a for each loop, which has a cleaner syntax. 我为每个循环使用了a,它的语法更简洁。
  • I am also iterating over results[0] instead of results , since you've got a nested array for whatever reason. 我还要遍历results[0]而不是results ,因为无论出于何种原因,您都有嵌套数组。
  • Because your function has an argument called results , it requires you to pass the global results in spite of it being a global. 因为您的函数有一个称为results的参数,但尽管它是一个全局的,但它仍需要您传递全局的results Another way to do this: 另一种方法是:

var results = [[a1,a2,a3,a4,a5]];

function checkWinner()
{ 
    for (var result in results[0]) 
    {
        if (result > 50)
        {
            winner = result;
            return;
        }
    }
}

checkWinner();

However, I would recommend against using global variables this way. 但是,我建议不要以这种方式使用全局变量。 Here's an explanation on why global variables are bad . 这是为什么全局变量不好的一种解释。 It's for C++, but it applies to JavaScript as well. 它适用于C ++,但也适用于JavaScript。

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

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