这个简单的 Javascript 函数有问题吗?

发布于 2024-11-28 10:05:24 字数 1068 浏览 2 评论 0原文

一旦我调用函数“meanValue”,智能感知就几乎停止工作,

我想我缩小了范围,但我不太明白。显然,函数“meanValue”有问题,因为在我在另一个函数中调用它之后,所有形式的智能感知都停止工作......这是我的代码。在我调用meanValue函数后,智能感知不适用于函数测试中的所有内容...

我不知道meanValue函数对我来说似乎很好? // 编辑:我已经缩小了范围。显然,任何具有 If(arr[0].length) 类型语法的函数,它几乎都会失败。需要注意的一件事是,函数运行良好并且调试良好,但由于某种原因,智能感知不喜欢这样。

有人知道还有什么方法可以检查某些内容是否已定义吗?我想检查我正在查看的数组类型,是否是多维数组。

谢谢!!! //

<script language="javascript" type="text/javascript">

    function meanValue(arr) {
        var mean;
        var sum = 0;

        if (arr[0].length) {
            for (var j = 0; j < arr[0].length; j++) {
                sum += arr[0][j];
            }
            mean = (sum) / arr[0].length;

        }
        else {
            for (var i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
            mean = (sum) / arr.length;


        }
        return mean;
    }

    function test(a, b) {
        var testing = 5;
        var oranges = meanValue(a);

     }
     var a = [1, 3, 4];
     var b = [4, 5, 6];

</script>

Intellisense pretty much stops working as soon as I call function "meanValue"

I think I narrowed it down but I can't quite figure it out. Apparently there is something wrong with the function "meanValue" because after I call it within another function, all forms of intellisense stop working...Here is my code. Intellisense doesnt work for everything inside function test after I call the meanValue function...

I have no clue the meanValue function seems fine to me??
//
EDIT: I've narrowed it down. Apparently any function where I have If(arr[0].length) type of syntax, it pretty much fails. One thing to note that is the functions run fine and debug fine but for some reason intellisense doesnt like this.

Anyone know what another way to check if something is defined or not? I want to check to see what kind of array I am looking at, if its a multidimensional array or not.

Thanks!!!
//

<script language="javascript" type="text/javascript">

    function meanValue(arr) {
        var mean;
        var sum = 0;

        if (arr[0].length) {
            for (var j = 0; j < arr[0].length; j++) {
                sum += arr[0][j];
            }
            mean = (sum) / arr[0].length;

        }
        else {
            for (var i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
            mean = (sum) / arr.length;


        }
        return mean;
    }

    function test(a, b) {
        var testing = 5;
        var oranges = meanValue(a);

     }
     var a = [1, 3, 4];
     var b = [4, 5, 6];

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-12-05 10:05:24

在 test() 中,您有一个变量testing,在 = 之后没有为其分配任何内容。这可能是问题之一。

谁在调用 test()?

In test() you have a variable testing, that has nothing assigned to it after =. That could be one of the problems.

Who is calling test()?

魔法唧唧 2024-12-05 10:05:24

我能够在我的 Netbeans 中重现该问题。

该问题似乎源于某些 IDE 在一个文件中混合了两种语言。 (这是一个包含一些 Javascript 的 .php 文件吗?)

出于某种原因,IDE 智能感知引擎尝试解析该代码块中的小于 (<) 符号,就像尝试验证 XML 一样,这JavaScript 则不然。所以,当然,它失败了。

尝试将该代码包装在 [CDATA 中——它应该可以解决问题。

因此,上面的示例经过修改后将是:

<script language="javascript" type="text/javascript">
//<![CDATA[ 
    function meanValue(arr) {
        var mean;
        var sum = 0;

        if (arr[0].length) {
            for (var j = 0 ; j < arr[0].length ; j++) 
            {
                sum += arr[0][j];
            }

            mean = (sum) / arr[0].length;
        }
        else 
        {
            for (var i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
            mean = (sum) / arr.length;
        }
        return mean;
    }

    function test(a, b) {
        var testing = 5;
        var oranges = meanValue(a);
    }

    var a = [1, 3, 4];
    var b = [4, 5, 6];
//]]>
</script>

I was able to reproduce the issue in my Netbeans.

The problem seems to stem from a mixing of two languages in one file for certain IDEs. (Is this a .php file with some Javascript in it?)

For some reason, the IDE intellisense engine is trying to parse the less-than (<) symbol in that chunk of code as if it were trying to validate XML, which Javascript isn't. So, of course, it's failing.

Try wrapping that code in [CDATA -- it should resolve the issue.

So the example above, modified, would be:

<script language="javascript" type="text/javascript">
//<![CDATA[ 
    function meanValue(arr) {
        var mean;
        var sum = 0;

        if (arr[0].length) {
            for (var j = 0 ; j < arr[0].length ; j++) 
            {
                sum += arr[0][j];
            }

            mean = (sum) / arr[0].length;
        }
        else 
        {
            for (var i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
            mean = (sum) / arr.length;
        }
        return mean;
    }

    function test(a, b) {
        var testing = 5;
        var oranges = meanValue(a);
    }

    var a = [1, 3, 4];
    var b = [4, 5, 6];
//]]>
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文