如何在JavaScript中通过引用传递变量?

我有三个变量,我想对它们执行一些操作,所以我想把它们放在一个for循环中,并对每个变量执行操作。

伪代码:

myArray = new Array(var1, var2, var3);
for (var x = 0; x < myArray.length; x++){
    // Do stuff to the array
    makePretty(myArray[x]);
}
// Now do stuff to the updated variables

最好的方法是什么?


当前回答

I've been playing around with syntax to do this sort of thing, but it requires some helpers that are a little unusual. It starts with not using 'var' at all, but a simple 'DECLARE' helper that creates a local variable and defines a scope for it via an anonymous callback. By controlling how variables are declared, we can choose to wrap them into objects so that they can always be passed by reference, essentially. This is similar to one of the Eduardo Cuomo's answer above, but the solution below does not require using strings as variable identifiers. Here's some minimal code to show the concept.

function Wrapper(val){
    this.VAL = val;
}
Wrapper.prototype.toString = function(){
    return this.VAL.toString();
}

function DECLARE(val, callback){
    var valWrapped = new Wrapper(val);    
    callback(valWrapped);
}

function INC(ref){
    if(ref && ref.hasOwnProperty('VAL')){
        ref.VAL++; 
    }
    else{
        ref++;//or maybe throw here instead?
    }

    return ref;
}

DECLARE(5, function(five){ //consider this line the same as 'let five = 5'
console.log("five is now " + five);
INC(five); // increment
console.log("five is incremented to " + five);
});

其他回答

其实很简单。问题在于,一旦传递了经典参数,您就会被限定在另一个只读区域。

解决方案是使用JavaScript的面向对象设计来传递参数。这与将参数放在全局/作用域变量中是一样的,但更好…

function action(){
  /* Process this.arg, modification allowed */
}

action.arg = [["empty-array"], "some string", 0x100, "last argument"];
action();

你也可以承诺一些东西来享受著名的连锁店: 这就是全部,像承诺一样的结构

function action(){
  /* Process this.arg, modification allowed */
  this.arg = ["a", "b"];
}

action.setArg = function(){this.arg = arguments; return this;}

action.setArg(["empty-array"], "some string", 0x100, "last argument")()

或者更好的是……

action.setArg(["empty-array"],"some string",0x100,"last argument").call()

使用解构这里是一个例子,我有3个变量,对每个我做多个操作:

如果value小于0,则改为0, 如果大于255,则改为1, 否则,将数字俯冲255以将0-255的范围转换为0-1的范围。

let a = 52.4, b = -25.1, c = 534.5;
[a, b, c] = [a, b, c].map(n => n < 0 ? 0 : n > 255 ? 1 : n / 255);
console.log(a, b, c); // 0.20549019607843136 0 1

其实有一个很好的解决方案:

function updateArray(context, targetName, callback) {
    context[targetName] = context[targetName].map(callback);
}

var myArray = ['a', 'b', 'c'];
updateArray(this, 'myArray', item => {return '_' + item});

console.log(myArray); //(3) ["_a", "_b", "_c"]

I've been playing around with syntax to do this sort of thing, but it requires some helpers that are a little unusual. It starts with not using 'var' at all, but a simple 'DECLARE' helper that creates a local variable and defines a scope for it via an anonymous callback. By controlling how variables are declared, we can choose to wrap them into objects so that they can always be passed by reference, essentially. This is similar to one of the Eduardo Cuomo's answer above, but the solution below does not require using strings as variable identifiers. Here's some minimal code to show the concept.

function Wrapper(val){
    this.VAL = val;
}
Wrapper.prototype.toString = function(){
    return this.VAL.toString();
}

function DECLARE(val, callback){
    var valWrapped = new Wrapper(val);    
    callback(valWrapped);
}

function INC(ref){
    if(ref && ref.hasOwnProperty('VAL')){
        ref.VAL++; 
    }
    else{
        ref++;//or maybe throw here instead?
    }

    return ref;
}

DECLARE(5, function(five){ //consider this line the same as 'let five = 5'
console.log("five is now " + five);
INC(five); // increment
console.log("five is incremented to " + five);
});

像字符串和数字这样的基本类型变量总是按值传递。 数组和对象通过引用或基于以下条件的值传递:

如果你正在设置一个对象或数组的值,它是通过值传递。 Object1 = {prop: "car"}; Array1 = [1,2,3]; 如果你正在改变一个对象或数组的属性值,那么它是引用传递。 中的object1。道具= "汽车"; Array1 [0] = 9;

Code

函数passVar(obj1, obj2, num) { 其中obj1。道具= "笔记本电脑";//将改变原来的 Obj2 ={道具:"电脑"};//不影响原 Num = Num + 1;//不影响原 } Var object1 = { 道具:“车” }; Var object2 = { 道具:“自行车” }; Var number1 = 10; passVar(object1, object2, number1); console.log(中的object1);//输出:对象{道具:"笔记本电脑"} console.log (object2);//输出:Object {prop: "bike"} console.log (number1);//输出:10