【问题标题】:Correct Way Of Achieving This Result?实现此结果的正确方法?
【发布时间】:2014-05-02 19:37:51
【问题描述】:

所以我目前有这段代码:

if(direction === "right" ) { var signOne = '-'; var signTwo = ''; }
if(direction === "left" ) { var signOne = ''; var signTwo = '-'; }

innerWork.not('.active').css({'left': signOne + '450px'}); // Move the not active text to the left
innerImg.not('.active').css({'left': signOne + '370px'}); // Move the not active images to the left

右边或左边通过函数的地方。我想要发生的事情,它按预期工作是,如果给定的函数是正确的,那么它会使两个左边的值减去,如果函数以“left”运行,那么它会给出正值。

正如我所说,这一切都有效,但 JSHint/Lint 会引发以下错误:a) 变量被定义了两次,b) 变量的使用超出了范围。有没有更简洁的方法来实现我想要的语法正确?

【问题讨论】:

  • 定义 if 块外的变量
  • 我需要在 if 块中使用它们,因为变量需要根据输入函数的左侧或右侧而改变。
  • 您需要在条件语句之外定义signOne和signTwo,并在内部分配它们......查看下面的所有答案

标签: jquery variables jslint jshint


【解决方案1】:

您需要在使用它们的同一范围内声明变量。

var signOne, signTwo;

if(direction === "right" ) { signOne = '-'; signTwo = ''; }
else if(direction === "left" ) { signOne = ''; signTwo = '-'; }

innerWork.not('.active').css({'left': signOne + '450px'}); // Move the not active text to the left
innerImg.not('.active').css({'left': signOne + '370px'}); // Move the not active images to the left

【讨论】:

  • 您的代码是正确的,但您的描述不正确:变量没有被破坏,这就是代码工作的原因。与 C/Java/等不同。花括号不定义范围。只有函数在 JavaScript 中定义范围。这就是为什么 jslint 抱怨变量被定义了两次。
【解决方案2】:
var signOne, signTwo;
if(direction === "right" ) { signOne = '-'; signTwo = ''; }
if(direction === "left" ) { signOne = ''; signTwo = '-'; }

innerWork.not('.active').css({'left': signOne + '450px'}); // Move the not active text to the left
innerImg.not('.active').css({'left': signOne + '370px'}); // Move the not active images to the left

【讨论】:

【解决方案3】:
var signOne, signTwo;

if(direction === "right" ) { signOne = '-'; signTwo = ''; }
if(direction === "left" ) { signOne = ''; signTwo = '-'; }

innerWork.not('.active').css({'left': signOne + '450px'}); // Move the not active text to the left
innerImg.not('.active').css({'left': signOne + '370px'}); // Move the not active images to the left

【讨论】:

    【解决方案4】:

    如果您正在学习 JS,我强烈建议您阅读 Douglas Crockford 的 JavaScript: The Good Parts。他是JSLint的原作者,他的JS风格规则被广泛认为是福音。

    关于您的问题,JSLint/Hint 给您错误的原因是您的变量声明在条件块中重复写入。更好的构造是在条件块之外初始化变量,然后在条件中定义它们。考虑以下几点:

    var signOne, signTwo;
    
    if (direction === "right") {
      signOne = '-';
      signTwo = '';
    } else if (direction === "left") {
      signOne = '';
      signTwo = '-';
    }
    
    innerWork.not('.active').css({'left': signOne + '450px'}); // Move the not active text to the left
    innerImg.not('.active').css({'left': signOne + '370px'}); // Move the not active images to the left
    

    【讨论】:

    • 小心。 Crockford 有一些不符合共识的观点,比如他对后增量的感受。
    • @Barmar ... 这就是为什么 JSLint 有 /*jslint plusplus:true */。在他没有选择退出的情况下,我还没有看到克罗克福德的规定在操作上比他所禁止的更糟糕的情况。他没有。说出你对 JSLint 原则的看法,伙计,至少它是一种精神。这是一个很好的起点。 2¢ 等等。我永远无法完全理解为什么一个出色的学习和质量工具会引起如此多的争议。 (这就是你说 JSHint 更好的地方,我认为。;^D)
    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2010-09-21
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    相关资源
    最近更新 更多