如何在 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
How to make function parameter constant in JavaScript?
提问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 arguments
objectin a const
initialisation:
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 const
is 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
/ 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.
正如@Andreas_Gnyp 所说,在 ES6 之前,JavaScript 中没有let
/ const
。(function(const a) {...}
一旦 ES6 出来并得到完全支持,也不会有。)如果你想使用const
,你可以实现你自己的const
特性,或者在一些第三方 ES6 到 ES5 编译器的帮助下开始使用 ES6 符号,比如Babel。
However, bear in mind that const
in ES6 notation does notmake 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 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 a
as 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 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.
首先,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 donttouchthis
is 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