[英]Javascript integer to string output not matching expected values
During some basic tests of JS usage I have been playing around with object generation / addition to an array. 在对JS用法进行一些基本测试期间,我一直在研究对象的生成/数组的添加。 I have so far used two different approaches for this problem:
到目前为止,我已经使用两种不同的方法来解决此问题:
While there are no errors at run time the value field on the global object property always outputs a 2
(Where the value is suppose to increment with the loop), while the function approach seemingly works without issue. 尽管在运行时没有错误,但全局对象属性上的value字段始终输出
2
(假定该值随循环而增加),而函数方法似乎没有问题。 Do you have any idea why this would be the case? 您知道为什么会这样吗?
The final output I generated was "[{"name":"Hello","value":2},{"name":"Hello World","value":1},{"name":"Hello","value":2},{"name":"Hello World","value":2},{"name":"Hello","value":2},{"name":"Hello World","value":3}]"
(from the console.log(JSON.stringify(testArray));
). 我生成的最终输出是
"[{"name":"Hello","value":2},{"name":"Hello World","value":1},{"name":"Hello","value":2},{"name":"Hello World","value":2},{"name":"Hello","value":2},{"name":"Hello World","value":3}]"
(来自console.log(JSON.stringify(testArray));
)。
I had expected an output of "[{"name":"Hello","value":0},{"name":"Hello World","value":1},{"name":"Hello","value":1},{"name":"Hello World","value":2},{"name":"Hello","value":2},{"name":"Hello World","value":3}]"
我期望输出为
"[{"name":"Hello","value":0},{"name":"Hello World","value":1},{"name":"Hello","value":1},{"name":"Hello World","value":2},{"name":"Hello","value":2},{"name":"Hello World","value":3}]"
The code for the relevant functions and object can be found below. 有关功能和对象的代码可以在下面找到。
Defining the global variable: 定义全局变量:
var globalVariable =
{
name: undefined,
value: undefined
}
Factory function: 工厂功能:
function globalVariableGen(name, valueInput)
{
return output =
{
name: name,
value: valueInput + 1
}
}
Array generation function: 数组生成功能:
function test2()
{
var i, testArray = [];
for (i = 0; i < 3; i++)
{
alert(i.toString());
globalVariable.name = "Hello";
globalVariable.value = i;
testArray.push(globalVariable);
testArray.push(globalVariableGen("Hello World", i));
}
console.log(JSON.stringify(testArray));
}
Kind regards, Shadow 亲切的问候,影子
This is because javascript is pass-by-reference and you're referring to the same globalVariable
which you add to the array multiple times. 这是因为javascript是传递引用,并且您引用的是多次添加到数组中的同一个
globalVariable
。 The array then contains multiple pointers to the globalVariable which is exactly the same. 然后,该数组包含多个指向globalVariable的指针,它们完全相同。
If you add a console.log(JSON.stringify(testArray)
into the loop you will see this behavior. 如果将
console.log(JSON.stringify(testArray)
到循环中,您将看到此行为。
> "[{"name":"Hello","value":0},{"name":"Hello World","value":1}]"
> "[{"name":"Hello","value":1},{"name":"Hello World","value":1},{"name":"Hello","value":1},{"name":"Hello World","value":2}]"
> "[{"name":"Hello","value":2},{"name":"Hello World","value":1},{"name":"Hello","value":2},{"name":"Hello World","value":2},{"name":"Hello","value":2},{"name":"Hello World","value":3}]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.