简体   繁体   English

JavaScript样式指南变量声明

[英]Javascript style guide variable declarations

Does anyone know why on the airbnb style guide this one is bad, while the other one is good? 有谁知道为什么在airbnb风格指南上这一个不好,而另一个很好呢? I would expect the 'name' variable to hoisted, like the previous example. 我希望像前面的示例一样,可以悬挂'name'变量。

thanks in advance. 提前致谢。

Here's a link. 这是一个链接。 https://github.com/airbnb/javascript#variables https://github.com/airbnb/javascript#variables

// bad
function() {
  var name = getName();

  if (!arguments.length) {
    return false;
  }

  return true;
}

// good
function() {
  if (!arguments.length) {
    return false;
  }

  var name = getName();

  return true;
}

Because if !arguments.length is true , you don't want to execute the rest of the function anyway, so there's no point in calling the function getName(); 因为如果!arguments.lengthtrue ,则无论如何都不希望执行其余函数,因此调用函数getName();毫无意义getName(); when assigning the variable that you'd never use when !arguments.length . 分配!arguments.length时永远不会使用的变量时。

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

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