6

How can I pass a string value by reference in javascript.

I want this kind of functionality.

    //Library.js
    function TryAppend(strMain,value)
    {
    strMain=strMain+value;
    return true;
    }

    //pager.aspx

    function validate()
    {
    str="Checking";
    TryAppend(str,"TextBox");
    alert(str); //expected result "Checking" TextBox
    //result being obtained "Checking"    
    }

How to do this. ?

Robusto
  • 31,447
  • 8
  • 56
  • 77
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • http://stackoverflow.com/questions/1308624/pass-a-string-by-reference-in-javascript – Haim Evgi Jul 14 '10 at 14:00
  • 1
    @haim evgi: I already saw that question. But that question was very confusing to me. I didn't get What ShowMe is as it seems to be a global variable. And I dont want to use global variable. – Shantanu Gupta Jul 14 '10 at 14:03
  • `strMain` is a local variable (in the context of first function). – Makram Saleh Jul 14 '10 at 14:04
  • I've just fallen into the situation where I need to do exactly like you (manipulate a string but return a boolean). I've always found JavaScript to be pathetic, but now I can officially claim it to be much worst than that. – Jeach May 03 '11 at 15:48

3 Answers3

4

You cannot pass a value by reference in JS. You could create an object with a function to do this for you:

function TryAppend(originalValue) {

    // Holds the value to return
    this.Value = originalValue;

    // The function joins the two strings
    this.Append = function (append) { 
        this.Value+=append; 
        return true;
    }

}

You can then use this in any method as follows:

function AnyProcedure() {

    var str = "Checking";
    var append = new TryAppend(str);
    if (append.Append("TextBox")) {
        alert(append.Value);  // Will give "CheckingTextBox"
    }

}

Each time you call append, the Value string will be appended to. I.e.

If you then did:

append.Append(" Foo");

append.Value would equal CheckingTextBox Foo.

djdd87
  • 67,346
  • 27
  • 156
  • 195
1

You need to return the String instead of true !!

    function TryAppend(strMain,value)  { 

    strMain=strMain+value; 

    return strMain; //you need return the  'String Value' to use in it another method

    } 


    //pager.aspx 


    function validate() { 

    str="Checking"; 

    str = TryAppend(str,"TextBox"); 

    alert(str); //expected result "Checking" TextBox 

    //result being obtained "Checking"     
    } 
1

Create a global variable (say gblstrMain) outside the function TryAppend and then set its value to strMain inside the function.

    var gblstrMain;

function TryAppend(strMain,value)
    {
    strMain=strMain+value;
    gblstrMain = strMain;
    return true;
    }

    //pager.aspx

    function validate()
    {
    str="Checking";
    TryAppend(str,"TextBox");
    str = gblstrMain;
    alert(str); //expected result "Checking" TextBox
    //result being obtained "Checking"    
    }

Since you are particular about "return true" in the TryAppend function, we can achieve by this workaround.

Vijey
  • 6,536
  • 7
  • 43
  • 50