谷歌JavaScript风格指南中值得注意的13点

发布时间:2018-11-11 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了谷歌JavaScript风格指南中值得注意的13点脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
对于那些不熟悉JavaScript的人来说,GOOGLE提供了一个编写JavaScript的样式指南,该指南列出了(Google相信的)编写干净、易懂的代码的最佳风格实践。 这些不是编写有效JavaScript的硬性规则,而是在整个文件中维护一致和吸引人的样式选择的限制。这对于JavaScript尤其有趣,JavaScript是一种灵活容的语言,允许各种各样的样式选择。 谷歌airbnb有两个最流行的风格指南在那里。如果你花很多时间写JS,我绝对建议你检查一下。 以下是我认为谷歌JS风格指南中最有趣和相关的十三条规则。 它们处理从激烈争论的问题(选项卡与空格,以及如何使用分号的有争议的问题)到让我惊的一些更模糊的规范。他们肯定会改变我写JS的方式。 对于每个规则,我将给出规范的概要,后面是详细描述规则的样式指南中的支持引号。在适用的情况下,我还将提供实践中的样式的示例,并将其与不遵循规则的代码进行比较。 使用空格,不使用制表符 除了行结束符序列之外,ASCII水平空格字符(0x20)是源文件中任何地方出现的唯一空格字符。这意味着……标签字符不用于缩进。 指南稍后指定您应该使用两个空格(不是四)来缩进。 For anyone who isn’t already familiar wITh it, Google puts out a style guide for writing JavaScript that lays out (what Google believes to be) the best stylistic PRactices for writing clean, understandable code. These are not hard and fast rules for writing valid JavaScript, only proscriptions for maintaining consistent and apPEaling style choices throughout your source files. This is particularly interesting for JavaScript, which is a flexible and forgiving language that allows for a wide VARiety of stylistic choices. Google and Airbnb have two of the most popular style guides out there. I’d definitely recommend you check out both of them if you spend much time writing JS. The following are thirteen of what I think are the most interesting and relevant rules From Google’s JS Style Guide. They deal with everything from hotly contested issues (tabs versus spaces, and the controversial issue of how SEMicolons should be used), to a few more obscure specifications which surprised me. They will definitely change the way I write my JS going forward. For each rule, I’ll give a summary of the specification, followed by a supporting quote from the style guide that describes the rule in detail. Where applicable, I’ll also provide an example of the style in practice, and contrast it with code that does not follow the rule. Use spaces, not tabs Aside from the line terminator sequence, the ASCII horizontal space character (0x20) is the only whitespace character that appears anywhere in a source file. This implies that… Tab characters are not used for indentation. The guide later specifies you should use two spaces (not four) for indentation.
// bad
function foo() {
∙∙∙∙let name;
}

// bad
function bar() {
∙let name;
}

// good
function baz() {
∙∙let name;
}
  分号是必需的。 每个语句必须用分号终止。禁止使用自动分号插入。 虽然我无法想象为什么有人反对这个想法,但是在JS中一贯使用分号正成为新的“空格对制表符”的争论。谷歌在这里为分号辩护。
// bad
let luke = {}
let leia = {}
[luke, leia].forEach(jedi => jedi.father = 'vader')
// good
let luke = {};
let leia = {};
[luke, leia].forEach((jedi) => {
  jedi.father = 'vader';
});
  不要使用ES6模块(还) 不要使用ES6模块(即导出和导入关键字),因为它们的语义尚未最终确定。请注意,一旦语义完全标准,将重新访问此策略。
// Don't do this kind of thing yet:
//------ lib.js ------
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
//------ main.js ------ import { square, diag } from 'lib'; 水平对齐被劝阻(但不被禁止) 这种做法是允许的,但通常是谷歌风格的劝阻。甚至不需要在已经使用的地方保持水平对齐。 水平对齐是在代码中添加可变数量的额外空格,以确保某些令牌直接显示在前一行中某些其他令牌的下面。
// bad
{
  tiny:   42,  
  longer: 435, 
};
// good
{
  tiny: 42, 
  longer: 435,
};
不再使用var 用const或Lew声明所有局部变量。默认情况下使用const,除非需要重新分配变量。不能使用var关键字。 我仍然看到人们在StAcExcel和其他地方的代码示例中使用VAR。我不知道有没有人会为它做一个案子,或者仅仅是一个老习惯的死亡。
// bad
var example = 42;
// good
let example = 42;
首选箭头函数 箭头函数提供了简明的语法,并解决了许多困难。在函数关键字上更喜欢箭头函数,特别是嵌套函数 老实说,我只是认为箭功能很棒,因为它们更简洁,更好看。事实证明,它们也起着非常重要的作用。
// bad
[1, 2, 3].map(function (x) {
  const y = x + 1;
  return x * y;
});

// good
[1, 2, 3].map((x) => {
  const y = x + 1;
  return x * y;
});
使用模板字符串而不是级联 在复杂的字符串连接中使用模板字符串(用‘分隔’),尤其是涉及多个字符串文字时。模板字符串可以跨多行。
// bad
function sayHi(name) {
  return 'How are you, ' + name + '?';
}

// bad
function sayHi(name) {
  return ['How are you, ', name, '?'].join();
}

// bad
function sayHi(name) {
  return `How are you, ${ name }?`;
}

// good
function sayHi(name) {
  return `How are you, ${name}?`;
}
不要对长字符串使用连线 不要在普通字符串或模板字符串文本中使用行延续(即,用反斜杠结束字符串文本内的行)。即使ES5允许这样做,如果斜线之后出现任何尾随的空格,那么它会导致棘手的错误,对读者来说不太明显。 有趣的是,这是谷歌和Airbnb不同意的规则(这里是Airbnb的规格)。 虽然Google建议连接更长的字符串(如下所示),但Airbnb的风格指南建议基本上不做任何事情,并允许长字符串继续下去,只要它们需要。
// bad (sorry, this doesn't show up well on mobile)
const longString = 'This is a very long string that \
    far exceeds the 80 column limit. It unfortunately \
    contains long stretches of spaces due to how the \
    continued lines are indented.';
// good
const longString = 'This is a very long string that ' + 
    'far exceeds the 80 column limit. It does not contain ' + 
    'long stretches of spaces since the concatenated ' +
    'strings are cleaner.';
“for of…”是循环的首选类型 使用ES6,语言现在有三种不同的for循环。所有的都可以使用,尽管在可能的情况下应该首选循环。 如果你问我,这很奇怪,但我想我会把它包括在内,因为Google声明for循环的首选类型非常有趣。 我总是觉得…在循环中对对象更好,而对于…更适合数组。一个“正确的工具”为正确的工作类型的情况。 虽然这里的Google规范不一定与这个想法相矛盾,但是了解他们特别喜欢这个循环还是很有趣的。 不要使用EVAL() 不要使用EVE或函数(…字符串)构造函数(除了代码加载程序)。这些特征是潜在危险的,在CSP环境中根本不起作用。 用于评估()的MDN页面甚至有一个部分叫“不要使用EVAL!”“
// bad
let obj = { a: 20, b: 30 };
let propName = getPropName();  // returns "a" or "b"
eval( 'var result = obj.' + propName );
// good
let obj = { a: 20, b: 30 };
let propName = getPropName();  // returns "a" or "b"
let result = obj[ propName ];  //  obj[ "a" ] is the same as obj.a
常数应该用下划线分隔 常数名使用康斯坦提格:所有大写字母,用下划线分隔单词。 如果你绝对确信变量不应该改变,你可以通过大写常量的名称来指示这个变量。这使得常量的不可变性显而易见,因为它在您的代码中被使用。 这个规则的一个显著例外是常数是函数作用域。在这种情况下,应该用骆驼写。
// bad
const number = 5;
// good
const NUMBER = 5;
One variable per declaration
Every local variable declaration declares only one variable: declarations such as let a = 1, b = 2; are not used.
// bad
let a = 1, b = 2, c = 3;
// good
let a = 1;
let b = 2;
let c = 3;
使用单引号,不使用双引号 普通字符串文字用单引号('')分隔,而不是双引号(“”)。 提示:如果一个字符串包含一个单引号字符,请考虑使用一个模板字符串来避免必须退出引用。
// bad
let directive = "No identification of self or mission."
// bad
let saying = 'Say it ain\u0027t so.';
// good
let directive = 'No identification of self or mission.';
// good
let saying = `Say it ain't so`;
正如我在开头所说的,这些不是命令。谷歌只是众多科技巨头之一,而这些只是推荐而已。 也就是说,看看Google这样的公司提出的风格建议很有意思,Google雇佣了很多聪明人,他们花费大量时间编写优秀的代码。 如果你想遵循“与Google兼容的源代码”的指导方针,你可以遵循这些规则,但是,当然,很多人不同意,你可以自由地拒绝任何或者所有这一切。 我个人认为有很多情况下,Airbnb的规格比Google的规格更有吸引力。不管你对这些特定规则采取什么立场,在编写任何代码时保持文体一致性仍然很重要。 觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的谷歌JavaScript风格指南中值得注意的13点全部内容,希望文章能够帮你解决谷歌JavaScript风格指南中值得注意的13点所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。