Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么要删除阵列的旧内容?_Javascript_Node.js - Fatal编程技术网

Javascript 为什么要删除阵列的旧内容?

Javascript 为什么要删除阵列的旧内容?,javascript,node.js,Javascript,Node.js,let sentenceArr=[['Teachers'、'are'、'cleer.]、['Developers'、'are'、'more'、'cleer'、'because'、'of'、'her'、'practice'、'experience.]]; for(设i=0;i=0){ console.log(“####splitsportionmarks”+instence_As_Arr[i]); console.log(“第1部分:”); 控制台日志(部件); 第[0]=i部分; parts[1

let sentenceArr=[['Teachers'、'are'、'cleer.]、['Developers'、'are'、'more'、'cleer'、'because'、'of'、'her'、'practice'、'experience.]];
for(设i=0;i=0){
console.log(“####splitsportionmarks”+instence_As_Arr[i]);
console.log(“第1部分:”);
控制台日志(部件);
第[0]=i部分;
parts[1]=instence_As_Arr[i]。切片(0,包含状态标记);
第[2]部分=作为第[i]部分的内容。切片(包含状态标记);
console.log(“第2部分:”);
控制台日志(部件);
log('splitParts1:');
控制台日志(拆分部分);
拆分部件。推动(部件);
log('splitParts2:');
控制台日志(拆分部分);
容器状态标记=(-1);
//部分=[];
}
}
}

}
这是因为您正在覆盖循环中的
部分。您将
parts
推入
splitParts
数组,但实际上对
parts
数组的引用将存储在那里。然后更改
parts
数组时,它将更改
splitParts
数组中的内容


如果将
parts
变量设置为内部循环的局部变量,则会得到所需的结果。

这是因为您正在覆盖循环中的
parts
。您将
parts
推入
splitParts
数组,但实际上对
parts
数组的引用将存储在那里。然后更改
parts
数组时,它将更改
splitParts
数组中的内容


如果将
parts
变量设置为内部循环的局部变量,则会得到想要的结果。

Javascript的工作方式定义了部分被覆盖的行为

传递值

在Javascript中,传递给函数的基元类型(int、boolean、String、null、undefined、Symbol)按值传递,这意味着创建一个新的内存空间,并在其中存储它们的副本

// demonstrating pass by value
function passByValuePrimitive(x) {
    // a new variable space is allocated in memory which holds the value 5 initially
    x = 6;// here you change the value of x to be 6 which points to the new variable created within this execution context.
    console.log(x)
}

var x = 5;
console.log(x); // value of x gets passed to the function.
passByValuePrimitive(x);
console.log(x); // here again in the global execution context, the value of x will be 5
通过参考传递

当我们考虑对象时,它们会被引用传递,这意味着对象的内存地址被传递,并且对该值的任何更改在程序的执行上下文中都是持久的。

// demonstrating pass by reference
function passByReference(x) {
    // the reference address of x is known to the function
    x.name = "Caesar";// here you change the value of x.name to be Caesar which points to the same memory address of the object x created in the global execution context.
    console.log(x)
}

var x = { name: "Julius"};
console.log(x); // Here the reference address of the object x in memory is passed to the function
passByReference(x);
console.log(x); // here in the global execution context, the value of x.name will be "Caesar"

希望能有帮助@SBylemans给出了一个清晰的答案。

Javascript的工作方式定义了部分被覆盖的行为

传递值

在Javascript中,传递给函数的基元类型(int、boolean、String、null、undefined、Symbol)按值传递,这意味着创建一个新的内存空间,并在其中存储它们的副本

// demonstrating pass by value
function passByValuePrimitive(x) {
    // a new variable space is allocated in memory which holds the value 5 initially
    x = 6;// here you change the value of x to be 6 which points to the new variable created within this execution context.
    console.log(x)
}

var x = 5;
console.log(x); // value of x gets passed to the function.
passByValuePrimitive(x);
console.log(x); // here again in the global execution context, the value of x will be 5
通过参考传递

当我们考虑对象时,它们会被引用传递,这意味着对象的内存地址被传递,并且对该值的任何更改在程序的执行上下文中都是持久的。

// demonstrating pass by reference
function passByReference(x) {
    // the reference address of x is known to the function
    x.name = "Caesar";// here you change the value of x.name to be Caesar which points to the same memory address of the object x created in the global execution context.
    console.log(x)
}

var x = { name: "Julius"};
console.log(x); // Here the reference address of the object x in memory is passed to the function
passByReference(x);
console.log(x); // here in the global execution context, the value of x.name will be "Caesar"

希望能有帮助@斯比勒曼斯给出了一个清晰的答案。

嗨,我建议你阅读保罗·爱尔兰的这篇文章或任何其他关于这个主题的文章。然后,您将能够在调试程序中跟踪程序的执行。请提供一个当前给定的代码不起作用。句子应改为句子。第一个for循环中的数组名称不匹配。请更正,我的“s”太多,变量名称错误。我添加了snippedHi,我建议您阅读Paul Irish的这篇文章或其他相关主题的文章。然后,您将能够在调试程序中跟踪程序的执行。请提供一个当前给定的代码不起作用。句子应改为句子。第一个for循环中的数组名称不匹配。请更正,我的“s”太多,变量名称错误。我加上了snippedthanks,很管用。这是否意味着for循环是一种函数?你说的“一种函数”是什么意思?在
for
循环中声明的变量是
for
循环的局部变量,大多数编程语言都是这样。如果答案可以接受,请接受;)谢谢你的回答。我符合我想问的问题。谢谢,这很有效。这是否意味着for循环是一种函数?你说的“一种函数”是什么意思?在
for
循环中声明的变量是
for
循环的局部变量,大多数编程语言都是这样。如果答案可以接受,请接受;)谢谢你的回答。我符合我试图提出的要求。