简体   繁体   English

如何使推入,移入,弹出和移入仅适用于javascript中的本地数组?

[英]How to make push, unshift, pop and shift to work only with local array in javascript?

I have a function to describe my problem: 我有一个函数来描述我的问题:

 function testingFunc(value) { var temp = value; console.log("temp before: " + JSON.stringify(temp)); console.log("arrayTest before: " + JSON.stringify(arrayTest)); temp.unshift(123); console.log("temp after unshift: " + JSON.stringify(temp)); console.log("arrayTest after unshift: " + JSON.stringify(arrayTest)); temp.push(456); console.log("temp after push: " + JSON.stringify(temp)); console.log("arrayTest after push: " + JSON.stringify(arrayTest)); temp.shift(); console.log("temp after shift: " + JSON.stringify(temp)); console.log("arrayTest after shift: " + JSON.stringify(arrayTest)); temp.pop(); console.log("temp after pop: " + JSON.stringify(temp)); console.log("arrayTest after pop: " + JSON.stringify(arrayTest)); return temp; } var arrayTest = [1,2,3,4,5]; var arrayTestTwo; arrayTestTwo = testingFunc(arrayTest); console.log("arrayTest after testingFunc: " + JSON.stringify(arrayTest)); console.log("arrayTestTwo: " + JSON.stringify(arrayTestTwo)); 

As you can see, arrayTest will change too if temp change by using push , unshift , pop and shift to edit it's data. 如您所见,如果通过使用pushunshiftpopshift编辑其数据来改变temparrayTest也会发生变化。

But I want those function to work ONLY with temp and ignore arrayTest . 但是我希望那些函数只能与temp并忽略arrayTest

Is it possible? 可能吗? Also is it possible to work with object contains functions? 也可以使用对象包含功能吗?

And why is this happen? 为什么会这样呢?

When you assign an array to a variable (or passing it as an argument to a function), you are only storing a reference to that array. 将数组分配给变量(或将其作为函数的参数传递)时,仅存储对该数组的引用 If two or more variables are referencing the same array, making changes to one will affect all the others as well: 如果两个或多个变量引用同一数组,则对一个变量进行更改也会影响所有其他变量:

 var original = []; var modified = original; console.log(original, modified); // [] [] modified.push(1, 2, 3); console.log(original, modified); // [1,2,3] [1,2,3] 

The way to solve this is to make a copy of the array. 解决此问题的方法是制作数组的副本 To make a copy of an array, you just call array .slice() : 要复制数组,只需调用array .slice()

 var original = []; var modified = original.slice(); console.log(original, modified); // [] [] modified.push(1, 2, 3); console.log(original, modified); // [] [1,2,3] 

You should have a look at concepts such as: 您应该看一下以下概念:

  1. Pure Functions: http://www.nicoespeon.com/en/2015/01/pure-functions-javascript/ 纯函数: http : //www.nicoespeon.com/cn/2015/01/pure-functions-javascript/
  2. Immutable Patterns: https://www.sitepoint.com/immutability-javascript/ 不变模式: https//www.sitepoint.com/immutability-javascript/
  3. Difference between value and reference : Is JavaScript a pass-by-reference or pass-by-value language? valuereference之间的区别: JavaScript是按引用传递还是按值传递语言?
  4. How to make a shallow copy or a deep copy: What is the difference between a shallow copy and a deep copy with JavaScript arrays? 如何制作浅表副本或深表副本: 浅表副本和具有JavaScript数组的深表副本之间有什么区别?

Basically primitives are always passed by value , object are always passed by reference . 基本上, primitives总是passed by valueobject总是passed by reference

 // Using Primitives: var a = "Hello"; var b = a; b += " World"; console.log('a', a); console.log('b', b); //Using References var a = { hello: "Hello" }; var b = a; b.hello += ' World'; console.log('a', a); console.log('b', b); // As you can see, because b is just a reference to a, editing b you're editing a too. 

So, following what I said above, you need to make a copy of that objects: 因此,按照我上面所说的,您需要复制这些对象:

 var a = { hello: 'Hello' }; var b = Object.assign(Object.create(null), a); //Object.assign is es-next, you need for a polyfill b.hello += ' World'; console.log('a', a); console.log('b', b); 

Making shallow copy of arrays is a bit more easy because there are many native methods: Array.prototype.map, Array.prototype.reduce, Array.prototype.filter, Array.prototype.concat, etc... ; 制作shallow copy of arrays要容易一些,因为有许多本机方法: Array.prototype.map, Array.prototype.reduce, Array.prototype.filter, Array.prototype.concat, etc...

Arrays, just like any object, are passed by reference. 数组,就像任何对象一样,都是通过引用传递的。 That means your temp is an alias to arrayTest . 这意味着您的临时文件是arrayTest别名 If you don't want to modify the array outside of the function, create a copy. 如果您不想在函数外部修改数组,请创建一个副本。

A shallow copy is all you need if you stick to .push(), .pop(), .shift() and .unshift(), because these will not change any of the array's elements, even if they are references themselves (non-primitive). 如果坚持使用.push()、. pop()、. shift()和.unshift(),则只需要浅表副本,因为即使它们本身是引用,它们也不会更改数组的任何元素(非-原始)。

For an array shallow copy, use .slice() 对于数组浅表副本,请使用.slice()

Reading the description will help: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice 阅读说明会有所帮助: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

There are some popular libraries to circumvent that problem for Arrays, Objects. 有一些流行的库可以解决数组,对象的问题。 etc., for example immutable.js from Facebook. 等等,例如来自Facebook的immutable.js

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM