【问题标题】:Porting C# to javascript is causing scoping issues将 C# 移植到 javascript 会导致范围问题
【发布时间】:2010-12-24 13:12:13
【问题描述】:

我正在尝试在 javaScript 中实现我在 C# 中找到的算法,但遇到了一些问题。

基本上我发现很难在 JS 中通过引用传递。我已将问题分解为以下示例代码,假设您有以下 C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            wordPoco[] myArray = new wordPoco[4] { 
                new wordPoco {word="This"},
                new wordPoco {word="is"},
                new wordPoco {word="a"},
                new wordPoco {word="test"}};

            Console.WriteLine("Values Before Recursion");
            MyRecursiveFunction(ref myArray);

            Console.Read();
        }

        private static void MyRecursiveFunction(ref wordPoco[] myArray)
        {
            if (myArray.Length <= 0) {
                Console.WriteLine();
                Console.WriteLine("Values After Recursion");
                return;
            }

            wordPoco[] newArray = (wordPoco[])myArray.Clone();

            Console.Write("ArrayValue: ");
            for (int j = 0; j < newArray.Length; j++)
            {
                Console.Write(newArray[j].word + " ");
            }
            Console.WriteLine();

            //popping in js
            wordPoco[] poppedArray = new wordPoco[newArray.Length - 1];
            Array.Copy(newArray, poppedArray, newArray.Length - 1);

            newArray = (wordPoco[])poppedArray.Clone();

            MyRecursiveFunction(ref newArray);

            myArray = (wordPoco[])newArray.Clone();

            Console.Write("ArrayValue: ");
            for (int j = 0; j < myArray.Length; j++)
            {
                Console.Write(myArray[j].word + " ");
            }
            Console.WriteLine();
        }
    }

    public class wordPoco {
        public string word;
    }
}

产生输出:

Values Before Recursion
ArrayValue: This is a test
ArrayValue: This is a
ArrayValue: This is
ArrayValue: This

Values After Recursion
ArrayValue:
ArrayValue:
ArrayValue:
ArrayValue:

下面的JS不应该产生同样的结果吗?

$(document).ready(function() {

    var debugBox = $("#debug");
    var myArray = [{ word: "This" }, { word: "is" }, { word: "a" }, { word: "test"}];

    debugBox.append("<b>Values Before Recursion</b><br/>");
    MyRecursiveFunction(myArray);

    function MyRecursiveFunction(myArray) {
        if (myArray.length <= 0) { debugBox.append("<b>Values After Recursion</b><br/>"); return false; }

        var newArray = $.extend(true, [], myArray);

        debugBox.append("ArrayValue: ");
        for (var j = 0; j < newArray.length; j++)
            debugBox.append(newArray[j].word + " "  );
        debugBox.append("<br/>");

        newArray.pop();
        arguments.callee(newArray);

        myArray = $.extend(true, [], newArray);

        debugBox.append("ArrayValue: ");
        for (var j = 0; j < myArray.length; j++)
            debugBox.append(myArray[j].word + " ");
        debugBox.append("<br/>");
    }
});

我得到的结果实际上是:

Values Before Recursion
Value: This is a test 
Value: This is a 
Value: This is 
Value: This 
Values After Recursion
Value: 
Value: This 
Value: This is 
Value: This is a 

非常感谢您的宝贵时间, 凯文

【问题讨论】:

    标签: c# javascript pass-by-reference


    【解决方案1】:

    最简单的答案是将其从 void 方法更改为返回数组的方法。

    因此 C# 声明将更改为:

    private static wordPoco[] MyRecursiveFunction(wordPoco[] myArray)
    

    当你递归时,你会这样做:

    newArray = MyRecursiveFunction(newArray);
    

    您只需在方法结束时返回 myArray

    这样可以在不使用传递引用的情况下轻松移植。

    【讨论】:

    • 感谢您的回答,不幸的是,我从 C# 移植的算法无法通过引用传递多个数组。
    • 我想我可以将它们捆绑在一起并返回大对象,但理想情况下我不想这样做......
    • @Kevin:这听起来不像是非常好的代码。 Ref 参数应该比较少见……听起来你应该应该将它们封装在一起。
    • 作为替代方案,给自己写一个类型“RefToArray”,它有操作“GetArray”和“SetArray”。然后传递一个 RefToArray 对象。将设置 ref 参数值的所有内容更改为调用 SetArray,并将获取 ref 参数值的所有内容更改为调用 GetArray。
    • @Jon,确实这不是最好的,我试图尽可能地移植它,这很愚蠢。我将接受你的建议,因为现在我从这个问题中退后一步,这是完全有道理的。谢谢你的帮助。干杯,K
    猜你喜欢
    • 2013-12-05
    • 2013-03-10
    • 2016-12-28
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多