【问题标题】:Solution to jshint "Better written in dot notation" when i have valid use of non-dot notation当我有效使用非点表示法时,jshint“最好用点表示法编写”的解决方案
【发布时间】:2015-01-16 15:24:13
【问题描述】:

当我得到一个字符串时,如何使用点表示法?

我正在编写一些代码来填充角度“x-editable”类型的控件。我有一个预定义的值数组,其中包含基于我的 webapi 服务将传回给我的字符串标识符。它发回一个字符串。基于这个字符串,我使用以下方法从我预先定义的数组中选择对象:

valuetoshow = myarray['stringFromWebApiCall'];

JSHINT 非常适合,因为它希望我使用点表示法。我明白为什么 JSHINT 告诉我这个,而且我也明白它告诉我哪些行,而且我知道如果我将代码更改为“answers.undergraduate = bigarray”之类的内容,它将修复 jshint。 当我在下面的代码中提供了一个字符串时,我只是不知道如何使用 .notation 访问数组。

javascript 中是否有某种方法可以让我使用字符串以点表示法查找某些内容?我已经习惯了 C# 和这种对变量的准类型奇数定义,这对我来说很棘手。

  • ['UNDERGRADUATE'] is better written in dot notation.
  • ['GRADUATE'] is better written in dot notation.
  • ['HONORARY'] is better written in dot notation.
  • ['DOCTORATE'] is better written in dot notation.
  • ['MASTERS'] is better written in dot notation.
  • ['UNDEFINED'] is better written in dot notation.

我应该尝试抑制错误吗?我应该在 api 结果上写一个丑陋的 switch 语句吗?

这是真正的代码

    answers['UNDERGRADUATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Create a network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Receive nursing guidance', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['GRADUATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['NURSE LEADER'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['HONORARY'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['DOCTORATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['MASTERS'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['UNDEFINED'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    if ($rootScope.constituent != undefined){
        if ($rootScope.constituent.InductedAs != undefined) {
            $scope.constituentPriorities = answers[$rootScope.constituent.InductedAs.toUpperCase()];
        } else {
            $scope.constituentPriorities = answers['UNDEFINED'];
        }   
    }

【问题讨论】:

  • 检查对象是否有属性。 stackoverflow.com/questions/135448/…
  • 嗯...代码中唯一需要括号表示的属性名称是NURSE LEADER,其他的都可以用点表示。
  • 你没有strings,你有括号内的primitives。它们是硬编码的,因此不是动态创建的。如果您需要括号内的变量,请删除引号。 硬编码,时,只有包含$, _, A-Z, a-z, 0-9之外的字符的属性名称才需要括号表示法

标签: javascript arrays syntax jshint


【解决方案1】:

有没有办法使用点符号来完成这样的事情

...是吗?

answers.UNDERGRADUATE = ...

澄清一下:您需要在代码上方编写内容,即数据的实际声明,如answers.UNDERGRADUATE。 JSHint 并没有抱怨这一行:

... answers[$rootScope.constituent.InductedAs.toUpperCase()];

显然,该行不能使用点表示法编写。 JSHint 抱怨的行是字面上写成answers['UNDEFINED']answers['UNDERGRADUATE'] 的行。这些是您需要修复以使 JSHint 静音的行。

【讨论】:

  • 我想你误读了这个问题。我不知道变量名。它的动态。
  • 我的 webapi 返回一个巨大的变量数组,包括 where array.InductedAs = 'UNDERGRADUATE' 等。
  • @CarCamp UM,answers[$r....] 不会发生错误,错误在于您在此答案状态下定义它的位置。
  • 为什么要通过 JSHint 从 API 运行结果?这个问题没有意义。您发布的错误表明 JSHint 对输入的文字字符串 answers['UNDERGRADUATE'] 有异常,而不是其他字符串 answers[variableName]。您不应该通过 JSHint 运行您的 API 响应。您也不应该返回 JavaScript。这就是 JSON 的用途。
  • @CarComp 你不能,而且 JSHint 也没有要求你这样做。再来一次,然后我不再回复,因为你没有听我的话:你得到的提示是关于 字面字符序列 answers['UNDERGRADUATE']。就这些。您需要重写特定系列的字符以读取answers.UNDEFINED 以使JSHint 静音。这些特定字符存在于您通过 JSHint 运行的代码中,否则 JSHint 不会给您提示。修复那些。 JSHint 无法为 answer[variableName] 之类的内容提供这样的提示。
猜你喜欢
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多