How can you do anything useful without mutable state?

后端 未结 18 1769
栀梦
栀梦 2020-11-28 17:12

I\'ve been reading a lot of stuff about functional programming lately, and I can understand most of it, but the one thing I just can\'t wrap my head around is stateless codi

18条回答
  •  萌比男神i
    2020-11-28 17:56

    JavaScript provides very clear examples of the different ways of approaching mutable or immutable state\values within its core because the ECMAScript specifications were not able to settle on a universal standard so one must continue to memorize or doublecheck which functions create a new object that they return or modify the original object passed to it. If your entire language is immutable then you know you are always getting a new (copied & possibly modified) result and never have to worry about accidentally modifying the variable before passing it into a function.

    Do you know which returns a new object and which changes the original of the following examples?

    Array.prototype.push()
    String.prototype.slice()
    Array.prototype.splice()
    String.prototype.trim()
    

提交回复
热议问题