简体   繁体   English

JavaScript整数到字符串输出不匹配期望值

[英]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: 到目前为止,我已经使用两种不同的方法来解决此问题:

  • A single global object which has fields varied and then added. 一个具有变化的字段然后添加的全局对象。
  • A factory function which generates the same object with an additional operation to show a slight difference (Addition of 1 to the value field) 工厂函数,它通过附加操作生成相同的对象,以显示细微的差异(值字段加1)

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.

相关问题 javascript regexp与预期的字符串不匹配 - javascript regexp not matching string as expected 带有“$”的字符串在Javascript中没有给出预期的输出 - string with "$" not giving expected output in Javascript JavaScript 如何将一组 integer 值的元素串起来? - How is JavaScript string a set of elements of integer values? Javascript将分隔符为“。”的具有多个整数值的字符串拆分为错误的值 - Javascript spliting a string with multiple integer values with delimiter as “.” retrns wrong values Javascript搜索匹配项,然后输出匹配单词所位于的整个字符串 - Javascript Search for a match and output the whole string that the matching word is apart of id、字符串或 integer 应与 eval() - id, string or integer was expected with eval() 使用字符串javascript regex匹配存储在变量或数组中的值 - matching values stored in variables or array with string javascript regex Javascript RegExp用于使用字符串中的$(美元符号)匹配不同的值 - Javascript RegExp for matching different values with $ (dollar sign) as part of the string Javascript:如何测试字符串是否包含两个值之间的 integer? - Javascript : How to test if a string contains an integer between 2 values? JavaScript base64string -&gt; 二进制值 -&gt; [Integer]... 性能提升 - JavaScript base64string -> binary values -> [Integer] ... performance improvement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM