Javascript 未定义不是对象(评估 myArray.length)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26898738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:29:41  来源:igfitidea点击:

Undefined is not an object (evaluating myArray.length)

javascripthtmlsafariquicksort

提问by Joshua Reddy

I've been working on a program that uses a quick sort algorithm to sort an array of numbers. This is my code:

我一直在研究使用快速排序算法对数字数组进行排序的程序。这是我的代码:

    var myArray=[8,2,5,6];

function quickSort(myArray)
{
    if (myArray.length === 0){
        return [];
    }

    var left=[];
    var right=[];
    var pivot= myArray[0];

    for (var i=1; i<myArray.length; i++){

        if (myArray[i]<pivot) {
            left.push(myArray[i]);

        }

        else {

            right.push(myArray[i]);
        }
    }

    return quickSort(left).concat(pivot, quickSort(right));
    document.getElementById().innerHTML = quickSort(left).concat(pivot, quickSort(right));
}

And here is my html

这是我的 html

<html>
<h1>QuickSorter</h1>
<button onclick="quicksort()">Quick Sort It!</button>
<script src="quicksort.js"> </script>
</html>

The console in safari keeps showing me this :

Safari 中的控制台不断向我显示:

TypeError: undefined is not an object (evaluating myArray.length)

I really don't understand at all why it isn't working. Any help would be appreciated.

我真的不明白为什么它不起作用。任何帮助,将不胜感激。

回答by Warren R.

If you are calling the function with

如果您正在调用该函数

quicksort()

you are not passing any arguments in. However, your function starts with

您没有传入任何参数。但是,您的函数以

function quickSort(myArray)
{
  if (myArray.length === 0){
    return [];
  }

Since no arguments are passed to quicksort, myArrayis left undefined. Undefined variables are not objects, and therefore can not have properties such as length.

由于没有参数传递给quicksort,myArray未定义。未定义的变量不是对象,因此不能具有诸如length.

EDIT:

编辑:

In order to call your function when you click the button, it is best to set the event listener in javascript rather than putting it in your HTML.

为了在您单击按钮时调用您的函数,最好在 javascript 中设置事件侦听器,而不是将它放在您的 HTML 中。

Give you button an ID name and remove the onclickattribute:

给你按钮一个 ID 名称并删除onclick属性:

<button id="sortButton">Quick Sort It!</button>

Then add the event listener in your JavaScript after your quickSortdefinition:

然后在quickSort定义之后在 JavaScript 中添加事件侦听器:

document.getElementById("sortButton").onclick = function(){quickSort(myArray);});

回答by user2717954

You dont pass the parameter to the function in your html onclick. So myArray ends up being undefined inside your func. Notice that myArray that u defined outside the function is not the same myArray inside of it defined as a parameter tp the function

您没有将参数传递给 html onclick 中的函数。因此 myArray 最终在您的 func 中未定义。请注意,您在函数外部定义的 myArray 与在函数内部定义为参数的 myArray 不同

回答by user9359064

Don't know if this is helpful here or not but document.getElementsByClassName is not returning any style,

不知道这在这里是否有帮助,但 document.getElementsByClassName 没有返回任何样式,

So while accessing elements via class name It'll give you same error what you mentioned in your question.

因此,在通过类名访问元素时,它会给您带来与您在问题中提到的相同的错误。

Below Code from my project.

下面是我项目中的代码。

document.getElementById("profile-main-div").style.display=="none".  //Working
document.getElementsByClassName("profile-main-div").style.display=="none".  //Not Working