如何在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
最好的方法是什么?
像字符串和数字这样的基本类型变量总是按值传递。
数组和对象通过引用或基于以下条件的值传递:
如果你正在设置一个对象或数组的值,它是通过值传递。
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
简单的对象
函数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);