哪些 JavaScript 数组函数正在发生变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9009879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which JavaScript Array functions are mutating?
提问by devios1
I am writing an Array-derived class in JavaScript and need to know which functions to overload so that I can be aware of changes made to the array.
我正在用 JavaScript 编写一个数组派生类,需要知道要重载哪些函数,以便我知道对数组所做的更改。
I know Array.push()
and Array.splice()
are mutating. Is there a definitive list of any others?
我知道Array.push()
并且Array.splice()
正在变异。是否有任何其他人的最终清单?
回答by Jonathan Lonowski
回答by Vinnie James
You can also use .concat()
, before using your mutation method, to ensure you are not mutating your arrays, eg
您还可以.concat()
在使用您的突变方法之前使用 ,以确保您不会改变您的数组,例如
const dontMutateMe = [4,5,1,2,3];
const sortArray = dontMutateMe.concat().sort(...)
回答by Sooraj
I found this website called Doesitmutate
我发现这个网站叫Dositmutate
Have the list of all functions - and tells whether it mutates or not.
拥有所有功能的列表 - 并说明它是否发生变异。