简体   繁体   English

为什么作为函数参数传递的Javascript数组可能会在顺序调用函数时丢失它的内容?

[英]Why a Javascript array passed as function parameter may lose it's content when the function is called sequentially?

I'm sitting here for a while now wondering why I'm losing an array parameter on a function call when calling it a second time. 我坐在这里一段时间,现在想知道为什么我在第二次调用它时在函数调用中丢失了一个数组参数。

The script I'm working on is mapped after CouchDB/PouchDB and stores items as JSON strings in multiple storages (including local storage). 我正在处理的脚本在CouchDB / PouchDB之后映射,并将项目作为JSON字符串存储在多个存储(包括本地存储)中。 Parameters are: 参数是:

_id         id of the item
_rev        revision string (version), counter and hash
_content    whatever content
_revisions  array of all prior hashes and current counter
_revs_info  all previous revisions of this item with status

I'm currently trying a PUT operation which by default updates an existing document. 我正在尝试PUT操作,默认情况下更新现有文档。 As I'm working with multiple storages, I also have a PUT SYNC , which "copy&pastes" versions of a document from one storage to another (with the goal having every version available on every storage). 当我使用多个存储时,我还有一个PUT SYNC ,它将文档的“复制和粘贴”版本从一个存储转移到另一个存储(目标是每个存储上都有可用的版本)。 I'm also keeping a separate file with a document tree, which stores all the version hashes. 我还使用文档树保存一个单独的文件,该文件树存储所有版本哈希值。 This tree file is updated on SYNCs using the _revs_info supplied with the PUT . 使用PUT提供的_revs_infoSYNCs上更新此树文件。

My problem is sequential SYNC PUTs . 我的问题是顺序SYNC PUTs The first one works, on the second I'm losing the _revs_info parameter. 第一个工作,第二个我失去了_revs_info参数。 And I don't know why... 我不知道为什么......

Here is my first call (from my QUnit module), which works fine: 这是我的第一个电话(来自我的QUnit模块),它工作正常:

o.jio.put({
    "content":'a_new_version',
    "_id":'myDoc',
    "_rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89",
    "_revs_info":[
        {"rev":"4-b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89","status":"available"},
        {"rev":"3-a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123","status":"deleted"},
        {"rev":fake_rev_1,"status":"deleted"},
        {"rev":fake_rev_0,"status":"deleted"}
        ],
    "_revisions":{
        "start":4,
        "ids":[
            "b5bb2f1657ac5ac270c14b2335e51ef1ffccc0a7259e14bce46380d6c446eb89",
            "a9dac9ff5c8e1b2fce58e5397e9b6a8de729d5c6eff8f26a7b71df6348986123",
            fake_id_1,
            fake_id_0
            ]}
    },
    function(err, response) { 
       // run tests
    });

However, when I call the same function a second time: 但是,当我第二次调用相同的函数时:

o.jio.put({
    "content":'a_deleted_version',
    "_id":'myDoc',
    "_rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61",
    "_revs_info":[   {"rev":"3-05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61","status":"deleted"},{"rev":"2-67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844","status":"deleted"},{"rev":fake_rev_2,"status":"deleted"}],
    "_revisions":{
        "start":3,
        "ids":[
            "05210795b6aa8cb5e1e7f021960d233cf963f1052b1a41777ca1a2aff8fd4b61",
            "67ac10df5b7e2582f2ea2344b01c68d461f44b98fef2c5cba5073cc3bdb5a844",
            fake_id_2
            ]}

    },
    function(err, response) {
      // run tests
    });

My script fails, because the _revs_info array does not include anything. 我的脚本失败,因为_revs_info数组不包含任何内容。 All other parameters and all random parameters I'm adding are transferred. 我正在添加的所有其他参数和所有随机参数都会被传输。 If I add a string or object instead of an array they also safely make it into my script alive. 如果我添加一个stringobject而不是一个array他们也可以安全地将它放入我的脚本中。

Array however... does not pass... 然而阵列......没有通过......

Question: 题:
I have been sitting on this for a few hours trying to nail down points I have not found, but I'm pretty clueless. 我一直坐在这里几个小时试图找到我没有找到的点,但我很无能为力。 So does anyone know of reasons, why arrays might lose their content, when passing them on as parameters in Javascript? 所以有人知道原因,为什么arrays可能会丢失其内容,当在Javascript中作为参数传递它们时?

Thanks! 谢谢!

EDIT : 编辑
I added a regular PUT after my first SYNC-PUT , which passed fine (without _revs_info being defined). 我在我的第一个SYNC-PUT PUT之后添加了一个常规PUT ,它传递正常(没有定义_revs_info )。

It's completely possible for a JavaScript function to mutate an array passed in. Consider this example: JavaScript函数完全有可能改变传入的数组。请考虑以下示例:

function removeAll(a) { a.splice(0); }

var arr = [1, 2, 3];
removeAll(arr);
console.log(arr); // empty array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在调用函数时无法正确传递参数? - Why the parameter can't be passed properly when function called? 当在JavaScript中执行传入的回调函数时,是否可以确定是否可以调用某个函数? - Is it possible to determine whether a certain function may be called when a passed-in callback function is executed in JavaScript? 为什么可以将参数传递给在 javascript 中没有参数的函数? - Why can argument(s) be passed to a function that has no parameter in javascript? 检查传递特定参数时是否调用函数 - Check if a function is called when a specific parameter is passed 带参数传递时无法调用Javascript函数 - Javascript function fails to be called when passed with an argument 块函数在javascript中作为参数传递时运行 - block function run when it is passed as parameter in javascript Function 作为未调用的参数传递 - Function passed as parameter not being called Javascript,将数组传递给函数时不再是数组 - Javascript, Array no longer array when passed into function 为什么Javascript docs将函数的参数定义为语法中的嵌套数组? - Why Javascript docs define function's parameter as a nested array in syntax? 传递给函数的对象中的Javascript数组为空 - Javascript array in object empty when passed to function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM