How to make function parameter constant in JavaScript?

后端 未结 8 1025
野的像风
野的像风 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".

    0 讨论(0)
  • 2020-12-13 13:22

    You can't make a parameter const. Use it as the initial value of a local variable:

    function constParam(a) {
        const const_a = a;
        ...
    }
    

    Note also that const is only supported in Internet Explorer as of IE11. See this compatibility table

    0 讨论(0)
  • 2020-12-13 13:27

    This is what I do:

    Instead of:

    function F(const a, const b, const c, const d, const e, const f, const g){ // Invalid Code
        // lorem
        // ipsum
    }
    

    ..Use:

    function F(){const[a, b, c, d, e, f, g] = arguments;
        // lorem
        // ipsum
    }
    
    0 讨论(0)
  • 2020-12-13 13:28

    We can use ES6 destructuring to create constants from params

    function test(...args) {
       const [a, b, c] = args;
    }
    
    0 讨论(0)
  • 2020-12-13 13:29
    function wrapper(i){
        const C=i
        return new Function("a","b", "return a+b+"+C)    
    }
    
    f100 = wrapper(100) //ƒ anonymous(a,b/*``*/) {return a+b+100} 
    f100(1,2) //OUTPUT 103
    
    f200 = wrapper(200) //ƒ anonymous(a,b/*``*/) {return a+b+200} 
    f200(1,2) //OUTPUT 203
    
    0 讨论(0)
  • 2020-12-13 13:32

    For immutable structures I believe you're looking for Immutable.js.


    As @Andreas_Gnyp is saying, until ES6 there is no let / const in JavaScript. (Nor there will be function(const a) {...} once ES6 is out and fully supported.) If you want to use const, you can either implement your own const feature, or start using ES6 notation with help of some third party ES6-to-ES5 compiler, such as Babel.

    However, bear in mind that const in ES6 notation does not make the variable immutable. E.g. const a = [1, 2]; a.push(3); is a completely valid program and a will become [1, 2, 3]. const will only prevent you from reassigning a, so that you can't do a = [] or a = {} or whatever once const a = [1, 2]; already defined (in that particular scope).

    function hasConstantParameters(...args) {
        const [a, b] = args;
    }
    

    Immutable.js will make sure that, when you define var a = fromJS([1, 2]); and pass a as a function parameter, in the receiving function a.push(3) will not affect a. Is this what you wanted to achieve?

    0 讨论(0)
提交回复
热议问题