How to make function parameter constant in JavaScript?

后端 未结 8 1085
野的像风
野的像风 2020-12-13 12:43

What I want to do is to use as many immutable variables as possible, thus reducing the number of moving parts in my code. I want to use \"var\" and \"let\" only when it\'s n

8条回答
  •  被撕碎了的回忆
    2020-12-13 13:19

    First of all, there are no constants in JS (until ECMAScript 6 proposal). You will have to use var to construct something like that. If you want to have "private" areas in your JS code, you use one of the features of the language, which are scopes.

    So, e.g, go like this:

     var kindaConstant = function(){
       var donttouchthis
       ... do something with it
       return whateveryouvedonewithit
     }
    

    In this case donttouchthis is not that easily mutable from "outside".

提交回复
热议问题