http://search.csdn.net/Expert/topic/1295/1295328.xml?temp=.1055414

解决方案 »

  1.   

    By Value vs. By Reference
    Numbers and Boolean values (true and false) are copied, passed, and compared by value. When you copy or pass by value, you allocate a space in computer memory and copy the value of the original into it. If you then change the original, the copy is not affected (and vice versa), because the two are separate entities.Objects, arrays, and functions are copied, passed, and compared by reference. When you copy or pass by reference, you essentially create a pointer to the original item, and use the pointer as if it were a copy. If you then change the original, you change both the original and the copy (and vice versa). There is really only one entity; the "copy" is not actually a copy, it's just another reference to the data.When comparing by reference, the two variables must refer to exactly the same entity for the comparison to succeed. For example, two distinct Array objects will always compare as unequal, even if they contain the same elements. One of the variables must be a reference to the other one for the comparison to succeed. To check if two Arrays hold the same elements, compare the results of the toString() method.Last, strings are copied and passed by reference, but are compared by value. Note that if you have two String objects (created with new String("something")), they are compared by reference, but if one or both of the values is a string value, they are compared by value.Note   Because of the way the ASCII and ANSI character sets are constructed, capital letters precede lowercase ones in sequence order. For example, "Zoo" compares as less than "aardvark." You can call toUpperCase() or toLowerCase() on both strings if you want to perform a case-insensitive match.
    Passing Parameters to Functions
    When you pass a parameter to a function by value, you are making a separate copy of that parameter, a copy that exists only inside the function. Even though objects and arrays are passed by reference, if you directly overwrite them with a new value in the function, the new value is not reflected outside the function. Only changes to properties of objects, or elements of arrays, are visible outside the function.
      

  2.   

    在 JavaScript 中,对数据的处理取决于该数据的类型。按值和按引用的比较
    Numbers 和 Boolean 类型的值 (true 和 false) 是按值来复制、传递和比较的。当按值复制或传递时,将在计算机内存中分配一块空间并将原值复制到其中。然后,即使更改原来的值,也不会影响所复制的值(反过来也一样),因为这两个值是独立的实体。对象、数组以及函数是按引用来复制、传递和比较的。当按地址复制或传递时,实际是创建一个指向原始项的指针,然后就像拷贝一样来使用该指针。如果随后更改原始项,则将同时更改原始项和复制项(反过来也一样)。实际上只有一个实体;“复本”并不是一个真正的复本,而只是该数据的又一个引用。当按引用比较时,要想比较成功,两个变量必须参照完全相同的实体。例如,两个不同的 Array 对象即使包含相同的元素也将比较为不相等。要想比较成功,其中一个变量必须为另一个的参考。要想检查两个数组是否包含了相同的元素,比较 toString() 方法的结果。最后,字符串是按引用复制和传递的,但是是按值来比较的。请注意,假如有两个 String 对象(用 new String("something") 创建的),按引用比较它们,但是,如果其中一个或者两者都是字符串值的话,按值比较它们。注意   鉴于 ASCII 和 ANSI 字符集的构造方法,按序列顺序大写字母位于小写字母的前面。例如 "Zoo" 小于 "aardvark"。如果想执行不区分大小写的匹配,可以对两个字符串调用 toUpperCase() 或 toLowerCase()。
    传递参数给函数
    按值传递一个参数给函数就是制作该参数的一个独立复本,即一个只存在于该函数内的复本。即使按引用传递对象和数组时,如果直接在函数中用新值覆盖原先的值,在函数外并不反映新值。只有在对象的属性或者数组的元素改变时,在函数外才可以看出。例如(使用 IE 对象模式):// 本代码段破坏(覆盖)其参数,所以
    // 调用代码中反映不出变化。
    function Clobber(param) 
    {
        // 破坏参数;在调用代码中
        // 看不到。
        param = new Object();
        param.message = "This will not work";
    }// 本段代码改变参数的属性,
    // 在调用代码中可看到属性改变。
    function Update(param)
    {
        // 改变对象的属性;
        // 可从调用代码中看到改变。
        param.message = "I was changed";
    }// 创建一个对象,并赋给一个属性。
    var obj = new Object();
    obj.message = "This is the original";// 调用 Clobber,并输出 obj.message。注意,它没有发生变化。
    Clobber(obj);
    window.alert(obj.message); // 仍然显示 "This is the original"。// 调用 Update,并输出 obj.message。注意,它已经被改变了。
    Update(obj);
    window.alert(obj.message); // 显示 "I was changed"。
    检验数据
    当按值进行检验时,是比较两个截然不同的项以查看它们是否相等。通常,该比较是逐字节进行的。当按引用进行检验时,是看这两项是否是指向同一个原始项的指针。如果是,则比较结果是相等;如果不是,即使它们每个字节都包含完全一样的值,比较结果也为不相等。按引用复制和传递字符串能节约内存;但是由于在字符串被创建后不能进行更改,因此可以按值进行比较。这样可以检查两个字符串是否包含相同的内容,即使它们是完全独立产生的。
      

  3.   

    我想我知道什么问题了,不只是按值传输那么简单,因为我用了string。假设下面代码:var sA='A';
    var sB='B';function myConcat(sInput1, sInput2)
    {
      sInput1+=sInput2;
    }myConcat(sA, sB);
    alert(sA);我希望alert显示的结果是'AB',那应该如何修改?
      

  4.   

    var sA='A';
    var sB='B';function myConcat(sInput1, sInput2)
    {
      return sInput1+sInput2;
    }sA = myConcat(sA, sB);
    alert(sA);
      

  5.   

    <script>
    var sA=new String("A");
    var sB=new String("B");
    var ary=new Array(sA,sB);function myConcat(arr)
    {
      arr[0]+=arr[1];
    }myConcat(ary);
    alert(ary[0]);
    </script>
      

  6.   

    zhanghk(lion) 的做法没错,不过我就是不想return,而想传递进去然后加工。
    不知道java的string机制是如何的,是不是好像.NET里面那样string看作对象,每一次修改都当作一个新的引用?