简体   繁体   English

覆盖函数参数值是一种好习惯吗?

[英]Is it good practice to override function parameter value?

In JavaScript consider I am trying to append a new value and return it.在 JavaScript 中考虑我正在尝试附加一个新值并返回它。

I have below example regarding overriding parameter value我有以下关于覆盖参数值的示例

The below function receives a string value as param and overriding the param with new value and returning it.下面的函数接收一个字符串值作为参数,并用新值覆盖参数并返回它。

 function test(value) { value = value + "hi"; return value; } console.log(test("Hello"));

The below function receives a string value as param.下面的函数接收一个字符串值作为参数。 I would like to append a new value and return it.我想附加一个新值并返回它。 So I assigned value to a local variable and then appended strong to a new variable and returning it.所以我给一个局部变量赋值,然后将 strong 附加到一个新变量并返回它。

 function test(value) { let temp = value; temp = value + "hi"; return temp; } console.log(test("Hello"));

I am calling it and passing value我正在调用它并传递值

  test(“Hello”);

Which one is recommended from above?上面推荐的是哪一个?

It's purely a matter of style. 这完全是一种风格问题。 Some people think you should leave parameter values alone, others think it's fine to change them.¹ 有些人认为你应该单独保留参数值,其他人认为改变它们是好的.¹

From a practical perspective, it doesn't cause any harm. 从实际角度来看,它不会造成任何伤害。 That is, there is no hidden side-effect to doing so. 也就是说,没有隐藏的副作用。 In particular, since JavaScript is purely pass-by-value, reassigning the parameter can't have any effect on whatever argument was used to fill in that parameter: 特别是,由于JavaScript纯粹是按值传递,因此重新分配参数不会对用于填充该参数的参数产生任何影响:

 function test(value) { value = value + "hi"; return value; } let a = "let's say "; let b = test(a); console.log(b); // "let's say hi" console.log(a === b); // false, `a` was not modified 

Your version with temp can be simpler, though: 你的temp版本可以更简单,但是:

function test(value) {
    let temp = value + "hi";
    return temp;
}

(or even (甚至

function test(value) {
    return value + "hi";
}

but I figure it's highly simplified for the question.) 但我认为这个问题非常简化。)


¹ (I happen to be in the latter camp, but that's neither here nor there.) ¹ (我碰巧在后一个阵营,但那既不在这里也不在那里。)

Yes, this is not at all wrong and is often done by many programmers across many languages. 是的,这一点都没有错,通常由许多语言的许多程序员完成。 It is a common practice. 这是一种常见的做法。

You can use it in cases where you want to use the parameter value inside the function but after making certain modifications to it. 您可以在想要在函数内部使用参数值但在对其进行某些修改之后使用它。

For example, I might want to add two numbers using a function add(a, b) where a and b can be strings or integers or floats . 例如,我可能想要使用函数add(a, b)添加两个数字add(a, b)其中ab可以是stringsintegersfloats

But just to be sure about it, I can define the add function in the following way: 但只是为了确定它,我可以通过以下方式定义add函数:

function add(a,b) {
 a = parseFloat(a);
 b = parseFloat(b);
 return a + b;
}

and this is perfectly fine. 这是完全正常 This way I can be always sure that there will be no exceptions thrown or in case parameters were passed as strings , it doesn't returns 12 (if I said add(1,2) ) when really it should have been 3 . 通过这种方式,我可以始终确保不会抛出任何异常,或者如果参数作为strings传递,它不会返回12 (如果我说add(1,2) ),实际上它应该是3

By making parameter overriding a common practice and incorporating it into your coding style, you spare the browser from creating or defining new variables just to modify those variable values. 通过使参数覆盖通用实践并将其合并到您的编码风格中,您可以使浏览器无需创建或定义新变量来修改这些变量值。 This might not mean much in small applications, but in large scale heavy applications, it might make a noticeable difference especially on low end devices. 这在小型应用程序中可能并不多,但在大型重型应用程序中,它可能会产生明显的差异,尤其是在低端设备上。

The short answer is: it's only a matter of style. 简短的回答是:这只是一种风格问题。

However, this isn't always right. 但是,这并不总是正确的。 When passing objects, they will be passed by reference, meaning that every change you'll make to the parameter will affect the original object: 传递对象时,它们通过引用传递,这意味着您对参数所做的每个更改都将影响原始对象:

 const obj = {originalValue: true}; function modifyObject(input) { input.originalValue = false; // The change happens here return input; // So it will take place regardless of this line } console.log('before:', obj); modifyObject(obj); // See? we don't even retrieve the return value console.log('after:', obj); 

If we were talking about Java, then creating a new variable would be good practice. 如果我们谈论Java,那么创建一个新变量将是一个好习惯。 As there is something called the Garbage Collector that collects unused variables, etc. and discards them. 因为有一些叫做垃圾收集器的东西收集未使用的变量等并丢弃它们。 So keeping a link to the original variable wouldn't allow the collector to discard the variable. 因此保持与原始变量的链接将不允许收集器丢弃该变量。 (I read this somewhere, but some people said to me it doesn't really work this way, so read more about this online if you want) (我在某个地方读过这篇文章,但有些人对我说这不是真的有用,所以如果你想在网上阅读更多关于这个的话)

In JavaScript, however, it doesn't really matter. 但是,在JavaScript中,它并不重要。 It depends on you. 这取决于你。 Your style. 你的风格。 It also depends on the situation as it can be useful sometimes. 它也取决于情况,因为它有时是有用的。 But really it doesn't really matter. 但实际上它并不重要。 Do as you like. 随便你怎么做。

If you want to simplify it you can do as @T.JCrowder said: 如果你想简化它,你可以像@ T.JCrowder那样说:

 function test(value){
   return value+ “hi”;
 } 

That's about it. 就是这样。

Using ES6 Template literals使用 ES6 模板文字

function test(value){
return `${value} hi`;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM