最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to pass a string value as a reference in javascript and change it over there - Stack Overflow

programmeradmin0浏览0评论

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. ?

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. ?

Share Improve this question edited Jul 14, 2010 at 14:01 Robusto 31.9k8 gold badges59 silver badges78 bronze badges asked Jul 14, 2010 at 13:58 Shantanu GuptaShantanu Gupta 21.1k56 gold badges186 silver badges293 bronze badges 4
  • stackoverflow./questions/1308624/… – Haim Evgi Commented Jul 14, 2010 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 Commented Jul 14, 2010 at 14:03
  • strMain is a local variable (in the context of first function). – Makram Saleh Commented Jul 14, 2010 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 Commented May 3, 2011 at 15:48
Add a ment  | 

3 Answers 3

Reset to default 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.

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"     
    } 

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.

发布评论

评论列表(0)

  1. 暂无评论