如何在 JavaScript 中使函数参数保持不变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30314457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:01:51  来源:igfitidea点击:

How to make function parameter constant in JavaScript?

javascriptecmascript-6constants

提问by shal

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 necessary.

我想要做的是尽可能多地使用不可变变量,从而减少代码中移动部分的数量。我只想在必要时使用“var”和“let”。

This won't work:

这行不通:

function constParam(const a){
    alert('You want me to '+a+'!');
}

Any ideas?

有任何想法吗?

回答by Bergi

Function parameters will stay mutable bindings (like var) in ES6, there's nothing you can do against that. Probably the best solution you get is to destructure the argumentsobjectin a constinitialisation:

var在 ES6 中,函数参数将保持可变绑定(如),对此您无能为力。您得到的最佳解决方案可能是在初始化中解构arguments对象const

function hasConstantParameters(const a, const b, const c, …) { // not possible
    …
}
function hasConstantParameters() {
    const [a, b, c, …] = arguments;
    …
}

Notice that this function will have a different arity (.length), if you need that you'll have to declare some placeholder parameters.

请注意,此函数将具有不同的元数 ( .length),如果您需要,则必须声明一些占位符参数。

回答by Barmar

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

你不能做一个参数const。将其用作局部变量的初始值:

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

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

另请注意,const自 IE11 起仅在 Internet Explorer 中受支持。请参阅此兼容性表

回答by Bugs Bunny

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

对于不可变结构,我相信您正在寻找Immutable.js



As @Andreas_Gnyp is saying, until ES6 there is no let/ constin 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 constfeature, or start using ES6 notation with help of some third party ES6-to-ES5 compiler, such as Babel.

正如@Andreas_Gnyp 所说,在 ES6 之前,JavaScript 中没有let/ const。(function(const a) {...}一旦 ES6 出来并得到完全支持,也不会有。)如果你想使用const,你可以实现你自己的const特性,或者在一些第三方 ES6 到 ES5 编译器的帮助下开始使用 ES6 符号,比如Babel

However, bear in mind that constin ES6 notation does notmake the variable immutable. E.g. const a = [1, 2]; a.push(3);is a completely valid program and awill become [1, 2, 3]. constwill onlyprevent 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).

但是,请记住,const在 ES6 符号中,不会使变量不可变。egconst a = [1, 2]; a.push(3);是一个完全有效的程序,a将变成[1, 2, 3]. const阻止您重新分配a,让你不能做a = []a = {}或任何一次const a = [1, 2];已经定义(在特定的范围内)。

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

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

Immutable.js将确保,当您定义var a = fromJS([1, 2]);a作为函数参数传递时,在接收函数中a.push(3)不会影响a. 这是您想要达到的目标吗?

回答by Logan R. Kearsley

There is no way to force a parameter to be immutable in JavaScript. You have to keep track of that yourself.

在 JavaScript 中没有办法强制参数不可变。你必须自己跟踪。

Just write in a style where you happen not to mutate variables. The fact that the language doesn't provide any facilities to forceyou to do so doesn't mean that you can't still do it anyway.

只需以一种您碰巧不会改变变量的风格来编写。该语言没有提供任何设施来强迫您这样做,这一事实并不意味着您仍然不能这样做。

回答by Pacerier

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
}

回答by Drenai

We can use ES6 destructuring to create constants from params

我们可以使用 ES6 解构从参数中创建常量

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

回答by Andreas Gnyp

First of all, there are no constants in JS (until ECMAScript 6 proposal). You will have to use varto 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.

首先,JS 中没有常量(直到 ECMAScript 6 提案)。您将不得不使用var来构建类似的东西。如果您想在您的 JS 代码中拥有“私有”区域,您可以使用该语言的一项功能,即范围。

So, e.g, go like this:

所以,例如,像这样:

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

In this case donttouchthisis not that easily mutable from "outside".

在这种情况下donttouchthis,从“外部”改变并不那么容易。

回答by sergey andriyaka

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