Which JavaScript Array functions are mutating?

 ̄綄美尐妖づ 提交于 2019-11-27 05:18:37

问题


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.

I know Array.push() and Array.splice() are mutating. Is there a definitive list of any others?


回答1:


You can find the list on MDN as Mutator methods (along with Accessor and Iteration methods):

  • copyWithin
  • fill
  • pop
  • push
  • reverse
  • shift
  • sort
  • splice
  • unshift



回答2:


You can also use .concat(), before using your mutation method, to ensure you are not mutating your arrays, eg

const dontMutateMe = [4,5,1,2,3];
const sortArray = dontMutateMe.concat().sort(...)



回答3:


I found this website called Doesitmutate

Have the list of all functions - and tells whether it mutates or not.



来源:https://stackoverflow.com/questions/9009879/which-javascript-array-functions-are-mutating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!