JavaScript Pass Variables Through Reference

前端 未结 4 875
刺人心
刺人心 2021-01-22 14:16

Is there an equivalent in JavaScript for PHP\'s reference passing of variables?

[PHP]:

function addToEnd(&$theRefVar,$str)
{
    $theRefVar.=$str;
}
$myVar=\"H         


        
相关标签:
4条回答
  • 2021-01-22 14:31

    try this:

    // function
    function func(param,callback){
        if(param){
         if(typeof callback=='function') {
             callback.call(this,param);
         }
         return true;
        }
        return false;
    }
    // calling function
    var variable=0;
    returnValue=func(10,function(reference){variable=reference});
    alert(returnValue+'/'+variable);
    
    0 讨论(0)
  • 2021-01-22 14:44

    Objects are passed as references.

       function addToEnd(obj,$str)
       {
          obj.setting += $str;
       }
    
       var foo = {setting:"Hello"};
       addToEnd(foo , " World!");
    
       console.log(foo.setting);                    // Outputs: Hello World!
    

    Edit:

    • As posted in comments below, CMS made mention of a great article.
    • It should be mentioned that there is no true way to pass anything by reference in JavaScript. The first line has been changed from "by reference" to "as reference". This workaround is merely as close as you're going to get (even globals act funny sometimes).
    • As CMS, HoLyVieR, and Matthew point out, the distinction should be made that foo is a reference to an object and that reference is passed by value to the function.

    The following is included as another way to work on the object's property, to make your function definition more robust.

       function addToEnd(obj,prop,$str)
       {
          obj[prop] += $str;
       }
    
       var foo = {setting:"Hello"};
       addToEnd(foo , 'setting' , " World!");
    
       console.log(foo.setting);                    // Outputs: Hello World!
    
    0 讨论(0)
  • 2021-01-22 14:47

    The other answers/comments describe the situation well enough, but I thought I'd offer and alternative if you need that style of functionality, by using a callback.

    var someText = "asd";
    addToEnd(someText, "fgh", function(val) { someText = val; });
    

    and

    function addToEnd(original, str, setValue)
    {
        setValue(original += str);
    }
    

    but a better solution would be

    var someText = "asd";
    someText = addToEnd(someText, "fgh");
    

    and

    function addToEnd(original, str)
    {
        return original += str;
    }
    
    0 讨论(0)
  • 2021-01-22 14:55

    In Javascript there is no passing variable by reference like in PHP. There is a possible workaround to do something similar.

    function addToEnd(obj, str)
    {
        obj.value += str;
    }
    
    myVar={value:"Hello"};
    
    addToEnd(myVar, " World");   
    alert(myVar.value); //Outputs: Hello World!
    

    In this example, what happens is that you pass an object to the function and inside of it, you are modifying the object (not the variable, the variable is still pointing to the same object). This is why this is not passing variable by reference has vol7ron incorrectly stated.

    0 讨论(0)
提交回复
热议问题