开发者

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

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?

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 w开发者_JS百科ay 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.


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

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;
console.log('Original:', b);
b = increaseB(b); // 4
console.log('Modified:', b);


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

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

let y = { x: 1 };
console.log('Original:', y);

change(y); // will make y.x = 2;

console.log('Modified:',y);


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.

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){}
)}


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


you can, but its bad practice,

Don’t Reassign Your Function Arguments

https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/

use destructuring instead.


You can replace the value:

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

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

    changeB(a, b);    
}


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


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜