Javascript closures and side effects in plain English? (separately)

前端 未结 6 2079
灰色年华
灰色年华 2020-12-05 00:49

I\'ve been reading some JavaScript books and I always hear about closures and side effects. For some reason I can\'t understand what they really are. Can anyone explain to m

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 01:26

    I am new to JavaScript, and will not try to talk of closures. However my newness to JavaScript makes me quite aware of the use of side effect that are impossible in my usual programming language (Erlang).

    Side effects seem to be a usual way to change state in JavaScript. Take for example this example from the w3cschools.com website:

    
    

    Here there is no input parameters or return value, instead the contents of the document get changed as they are global in scope to the function. If you were to write this in Erlang for example, the document would be passed in as a parameter, and the new document state would be returned. A person reading the calling program would see a document passed in and an altered document being returned.

    Seeing functions called that to not return explicit new state should alert to programmer to probable use of sideeffects.

提交回复
热议问题