Javascript 是否可以更改函数参数的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5292159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:30:36  来源:igfitidea点击:

Is it possible to change the value of the function parameter?

javascriptjqueryfunctionparametersparameter-passing

提问by Ben

I have one function with two parameters, for example function (a,b). I want to within the function, take the value of b, and replace that with c. I was thinking of something like $(b).replaceWith(c), but it didn't work out. I know I can create a new variable within the function with the new value, but is it possible to change the value of b itself? Is something like this possible. Or are the parameters set in stone?

我有一个带有两个参数的函数,例如function (a,b). 我想在函数中,取 b 的值,然后用 c 替换它。我在想类似的东西$(b).replaceWith(c),但没有成功。我知道我可以使用新值在函数内创建一个新变量,但是是否可以更改 b 本身的值?这样的事情可能吗。或者参数是一成不变的?

Let me try to explain what I am trying to do. There are three functions, one overarching function and then two functions within it which are triggered by toggle event. And I want the second function to do something to get a value and pass it on to the third function. So here would be the code

让我试着解释一下我想要做什么。共有三个功能,一个是总体功能,然后是其中的两个由切换事件触发的功能。我希望第二个函数做一些事情来获取一个值并将它传递给第三个函数。所以这里是代码

function(a,b){

    $('selector').toggle(

        //I want this to gather a value and store it in a variable
        function(){},

        //and I want this to accept the variable and value from the previous function
        function(){}
    )}

The only way I can think of doing this is to add a parameter c for the overarching function and then modify it with the first function and have the new value pass on to the second function.

我能想到的唯一方法是为总体函数添加一个参数 c,然后用第一个函数修改它,并将新值传递给第二个函数。

回答by BoltClock

You cannot pass explicitly by reference in JavaScript; however if bwere an object, and in your function you modified b's property, the change will be reflected in the calling scope.

在 JavaScript 中不能通过引用显式传递;但是,如果b是一个对象,并且在您的函数中修改了b的属性,则更改将反映在调用范围中。

If you need to do this with primitives you need to return the new value:

如果您需要使用原语执行此操作,则需要返回新值:

function increaseB(b) {
    // Or in one line, return b + 1;
    var c = b + 1;
    return c;
}

var b = 3;
b = increaseB(b); // 4

回答by mohamed.ahmed

You cannot change the value of an integer or a string in JavaScript BUTyou can change the value of the attributes of an object for example

您不能在 JavaScript中更改整数或字符串的值,您可以更改对象的属性值,例如

function change(x){
x.x=2;
}

when call with

当与

var y = {x:1};
change(y);

will make y.x = 2;

将使 yx = 2;

回答by Cyoce

If the variable is in the global scope, you can set it as follows:

如果变量在全局范围内,可以如下设置:

function modify(name,newVal){
  window[name] = newVal;
}

Then, try this:

然后,试试这个:

var foo = 10;
modify('foo','bar');
console.log(foo); // bar

回答by New Guy

When passing an object, you are passing a reference to that object. So when modifying an object that was passed into a function you are modifying that original object. Therefore you can create a very simple object containing a single variable that will allow you to carry the value around from one function to the next.

传递对象时,您传递的是对该对象的引用。因此,当修改传递给函数的对象时,您正在修改该原始对象。因此,您可以创建一个包含单个变量的非常简单的对象,该对象将允许您将值从一个函数传递到另一个函数。

There is one thing that should be noted about the way you are putting these function calls into the parameter list of another function. I am not certain what the order of execution is for evaluating a parameter list. Perhaps someone who knows more about Javascript can answer that. However, not all languages will evaluate a parameter list left to right which means you may find the second function executes before the first. Again though, I'm not certain of the order of execution for Javascript so that may not be a concern.

关于将这些函数调用放入另一个函数的参数列表的方式,应该注意一件事。我不确定评估参数列表的执行顺序是什么。也许对 Javascript 有更多了解的人可以回答这个问题。但是,并非所有语言都会从左到右评估参数列表,这意味着您可能会发现第二个函数在第一个函数之前执行。尽管如此,我不确定 Javascript 的执行顺序,所以这可能不是问题。

function(a,b){
    var object_c = {
        c: 10
    };

    $('selector').toggle(

    //send object_c in to gather a value and store it in the "c" field
    function(object_c){},

    //send the same object_c object into the second function
    function(object_c){}
)}

回答by davin

b = c; // very little is set in stone in javascript

回答by Alistair Laing

You can replace the value:

您可以替换该值:

function myFunc() {
    var a = 1, b = 2, c = 3;

    function changeB(a, b) {
        b = c;
    }

    changeB(a, b);    
}

回答by Piotr Salaciak

Do You mean:

你的意思是:

// fun is Your actual function (a,b)

var actualFunction = fun;

fun = function (a,b){
   var c = 10;
   actualFunction(a,c);
}

Let me explain what is going onhere: You are creating a variable (actualFunction) and let it hold pointer to Your function(a,b) (named here "fun"). Then You are setting new definition for "fun" so it's calling the old one with the parameters You like.

让我解释一下这里发生了什么:您正在创建一个变量 (actualFunction) 并让它持有指向您的函数 (a,b) 的指针(此处命名为“fun”)。然后,您正在为“乐趣”设置新定义,因此它使用您喜欢的参数调用旧定义。

I'm not quite sure if it run without exceptions, but please try it and let us know.

我不太确定它是否无一例外地运行,但请尝试一下并告诉我们。