如何在 javascript 中传递字符串值作为引用并在那里更改它

发布于 2024-09-09 02:30:20 字数 409 浏览 5 评论 0原文

如何在 javascript 中通过引用传递字符串值。

我想要这样的功能。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-09-16 02:30:20

在 JS 中不能通过引用传递值。您可以创建一个带有函数的对象来为您执行此操作:

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

}

然后您可以在任何方法中使用它,如下所示:

function AnyProcedure() {

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

}

每次调用追加时,都会附加值字符串。即,

如果您这样做:

append.Append(" Foo");

append.Value 将等于CheckingTextBox Foo

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.

待天淡蓝洁白时 2024-09-16 02:30:20

您需要返回String而不是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"     
    } 

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"     
    } 
风筝有风,海豚有海 2024-09-16 02:30:20

在函数 TryAppend 外部创建一个全局变量(例如 gblstrMain),然后在函数内部将其值设置为 strMain。

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

由于您特别注重 TryAppend 函数中的“return true”,因此我们可以通过此解决方法来实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文