如何在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

最好的方法是什么?


当前回答

简单的对象

函数foo(x) { //使用其他上下文函数 //修改' x '属性,增加值 x.value + +; } //初始化' ref '为对象 Var = { // ' value '在' ref '变量对象中 //初始值为' 1 ' 值:1 }; //用对象值调用函数 foo (ref); //再次调用带有对象值的函数 foo (ref); console.log (ref.value);//打印"3"


自定义对象

对象rvar

/** * Aux function to create by-references variables */ function rvar(name, value, context) { // If `this` is a `rvar` instance if (this instanceof rvar) { // Inside `rvar` context... // Internal object value this.value = value; // Object `name` property Object.defineProperty(this, 'name', { value: name }); // Object `hasValue` property Object.defineProperty(this, 'hasValue', { get: function () { // If the internal object value is not `undefined` return this.value !== undefined; } }); // Copy value constructor for type-check if ((value !== undefined) && (value !== null)) { this.constructor = value.constructor; } // To String method this.toString = function () { // Convert the internal value to string return this.value + ''; }; } else { // Outside `rvar` context... // Initialice `rvar` object if (!rvar.refs) { rvar.refs = {}; } // Initialize context if it is not defined if (!context) { context = this; } // Store variable rvar.refs[name] = new rvar(name, value, context); // Define variable at context Object.defineProperty(context, name, { // Getter get: function () { return rvar.refs[name]; }, // Setter set: function (v) { rvar.refs[name].value = v; }, // Can be overrided? configurable: true }); // Return object reference return context[name]; } } // Variable Declaration // Declare `test_ref` variable rvar('test_ref_1'); // Assign value `5` test_ref_1 = 5; // Or test_ref_1.value = 5; // Or declare and initialize with `5`: rvar('test_ref_2', 5); // ------------------------------ // Test Code // Test Function function Fn1(v) { v.value = 100; } // Test function test(fn) { console.log(fn.toString()); console.info(fn()); } // Declare rvar('test_ref_number'); // First assign test_ref_number = 5; test(() => test_ref_number.value === 5); // Call function with reference Fn1(test_ref_number); test(() => test_ref_number.value === 100); // Increase value test_ref_number++; test(() => test_ref_number.value === 101); // Update value test_ref_number = test_ref_number - 10; test(() => test_ref_number.value === 91);

其他回答

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

如果你想通过引用传递变量,一个更好的方法是通过在对象中传递参数,然后使用window开始更改值:

window["varName"] = value;

例子:

// Variables with first values
var x = 1, b = 0, f = 15;


function asByReference (
    argumentHasVars = {},   // Passing variables in object
    newValues = [])         // Pass new values in array
{
    let VarsNames = [];

    // Getting variables names one by one
    for(let name in argumentHasVars)
        VarsNames.push(name);

    // Accessing variables by using window one by one
    for(let i = 0; i < VarsNames.length; i += 1)
        window[VarsNames[i]] = newValues[i]; // Set new value
}

console.log(x, b, f); // Output with first values

asByReference({x, b, f}, [5, 5, 5]); // Passing as by reference

console.log(x, b, f); // Output after changing values

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

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

由于我们没有javascript的引用传递功能,唯一的方法是让函数返回值,并让调用者分配它:

So

"makePretty(myArray[x]);"

应该是

"myArray[x] = makePretty(myArray[x]);"

这是在函数内部需要赋值的情况下,如果只需要突变,那么传递对象并突变它就足够了

我完全明白你的意思。同样的事情在Swift中是没有问题的。底线是使用let,而不是var。

原语是按值传递的,但在迭代时var i的值没有复制到匿名函数中,这一点至少令人惊讶。

for (let i = 0; i < boxArray.length; i++) {
  boxArray[i].onclick = function() { console.log(i) }; // Correctly prints the index
}