Which JavaScript Array functions are mutating?

后端 未结 3 974
难免孤独
难免孤独 2020-12-07 18:31

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 Arra

相关标签:
3条回答
  • 2020-12-07 19:04

    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
    0 讨论(0)
  • 2020-12-07 19:08

    I found this website called Doesitmutate

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

    0 讨论(0)
  • 2020-12-07 19:09

    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(...)
    
    0 讨论(0)
提交回复
热议问题