简体   繁体   English

摆脱无参数重新分配错误(eslint)的最佳方法是什么?

[英]What is the best way to get rid of no-param-reassign error (eslint)?

I'm currently trying out eslint on this code and ran into an error (see screenshot). 我目前正在尝试在此代码上作弊,并遇到错误(请参见屏幕截图)。 I'm not sure of what is the best way to get rid of the error. 我不确定消除错误的最佳方法是什么。

code snippet: 代码段:

let pathFor = ( path, view ) => {
  if ( path.hash ) {
    view = path;
    path = view.hash.route;
    delete view.hash.route;
  }

attempt to get rid of error (which works but not sure if it's the best way): 尝试摆脱错误(该方法有效,但不确定是否是最佳方法):

let pathFor = ( pathData, viewData ) => {
  if ( pathData.hash ) {
    view = pathData;
    path = view.hash.route;
    delete view.hash.route;
  }

Here is the screenshot of eslinting in sublime 这是高高在上护送的屏幕截图

在此处输入图片说明

Lots of ways to do it. 有很多方法可以做到这一点。 The linter is throwing at error because you're mutating a parameter, which can appear obfuscated to people reading your code, or cause other problems down the line. lint会出错,因为您正在更改参数,该参数可能会使阅读代码的人感到困惑,或导致其他问题。 A more "functional style" of programming is basically what the linter is recommending (creating a new variable instead of mutating an old one), and probably rightly so. 基本上,linter推荐的是一种更具“功能性”的编程方式(创建一个新变量而不是对一个旧变量进行变异),并且可能是正确的。 A method I've used in the past is with "Hungarian case": 我过去使用的一种方法是使用“匈牙利案例”:

let pathFor = (_path, _view) => {
  if (_path.hash) {
    view = _path;
    path = view.hash.route;
    delete view.hash.route;
  }

However, the little underscores can be arbitrary in their meaning depending on who's reading your code. 但是,下划线的含义可以是任意的,具体取决于谁正在阅读您的代码。

But, I think the way you have it is just fine. 但是,我认为您拥有的方式就很好。 As Phil Karlton said: 正如Phil Karlton所说:

There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两件难事:缓存无效和命名。

I think this question is more a question of "best naming practices" more than anything. 我认为这个问题比什么都更是“最佳命名惯例”的问题。 If that's the case, I wouldn't waste too much mental energy on it. 如果是这样的话,我不会在上面浪费太多的精力。

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

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