JavaScript最佳做法

深度JavaScript (Deep JavaScript)

These are what I consider to be some JavaScript best practices that can improve the quality and clarity of your code.

这些是我认为可以提高代码质量和清晰度JavaScript最佳实践。

句法 (Syntax)

使用2个空格进行缩进 (Use 2 spaces for indentation)

In recent years there has been a massive switch from your to two spaces for indentation. For example, two spaces, not longer and no tabs — used by Google, npm, Node.js, Idiomatic, Felix.

近年来,已经从您的空间大幅度切换到两个空间进行缩进。 例如,Google,npm,Node.js,Idiomatic和Felix使用两个空格,不再是空格,也没有制表符。

Keep in mind that don’t use tabs for indentation because tabs are displayed so differently between applications and OS.

请记住,请勿使用制表符进行缩进,因为在应用程序和操作系统之间,制表符的显示方式会有所不同。

除了避免转义,我对字符串使用单引号 (I use single quotes for strings except to avoid escaping)

You’ve seen both 'single quotes' and "double quotes" used for writing strings in JavaScript.

您已经看到了用于在JavaScript中编写字符串的'single quotes'"double quotes"

'abc' === "abc"

But I prefer to use single quotes to avoid escaping.

但是我更喜欢使用单引号来避免转义。

我在关键字之后和左括号之前添加一个空格 (I add a space after keywords and before an opening parenthesis)

In written English, there are no spaces after an opening parenthesis and before a closing parenthesis. I also apply this method to JavaScript.

在书面英语中,左括号后和右括号前没有空格。 我也将此方法应用于JavaScript。

There a space after the keyword function.

关键字function后有一个空格。

For anonymous functions, I also add a space as follows.

对于匿名函数,我还添加了一个空格,如下所示。

我在逗号后添加空格 (I add spaces after commas)

Commas have no meaning, but they help us to see the structure and therefore the meaning of the sentence. Put a space after a comma.

逗号没有意义,但是它们可以帮助我们了解句子的结构和含义。 在逗号后放置一个空格。

我将其他语句与花括号放在同一行 (I kept else statements on the same line as their curly braces)

我将条件运算符放在括号中 (I put the conditional operator in parentheses)

This helps with reading. The parentheses are useless for the execution. But useless for the execution doesn’t mean useless for the good reading of your code. You should leave them if it makes more sense to read.

这有助于阅读。 括号对于执行无用。 但是对于执行无用并不意味着对代码的良好阅读无用。 如果阅读起来更有意义,则应将其保留。

变数 (Variables)

我每行声明一个变量 (I declare one variable per line)

Don’t declare multiple variables with a single declaration.

不要用一个声明来声明多个变量。

I declare one variable per line since it is simple to delete, insert and rearrange lines and the lines are automatically indented correctly.

我声明每行一个变量,因为它很容易删除,插入和重新排列行,并且行会自动正确缩进。

不要在两个不同的块中两次声明相同的变量 (Don’t declare the same variable twice in two different blocks)

I don’t do this following.

我下面不这样做。

It should be written the below way.

应该以下面的方式写。

我用/* global */声明浏览器全局 (I declare browser globals with a /* global */ comment)

Exceptions are: window, document, and navigator.

例外是: windowdocumentnavigator

/* global
ADSAFE, report, jslint
*/

使用对象的规则 (The rules for using Object)

构造函数名称必须以大写字母开头 (Constructor names must begin with a capital letter)

A constructor name should start with an uppercase letter as follows.

构造函数名称应以大写字母开头,如下所示。

创建实例时,我总是使用构造函数和关键字'new' (I always use constructors and keyword ‘new' when creating an instance)

It is important to use constructors because it will protect you against forgetting the new operators for instantiation in the strict mode.

使用构造函数很重要,因为它可以保护您避免在strict mode忘记new用于实例化的运算符。

It also helps my code better fits into the JavaScript mainstream and is more likely to be portable between frameworks.

它还可以帮助我的代码更好地适合JavaScript主流,并且更有可能在框架之间移植。

In case that a constructor function has no arguments, I still write parentheses.

如果构造函数没有参数,我仍会写上括号。

我给私有属性的名称加上下划线 (I prefix the names of private properties with an underscore)

If you want an object’s private data to be completely safe, you have to use closures but it makes the code becomes more complicated.= and slowers so I prefer to add an underscore for private properties.

如果要使对象的私有数据完全安全,则必须使用闭包,但这会使代码变得更加复杂。=且速度较慢,因此我更愿意为私有属性添加下划线。

其他规定 (Other rules)

我总是使用===而不是== (I always use === instead of ==)

The === operator will not do the conversion, so if two values are not the same type === will simply return false.

===运算符将不会进行转换,因此,如果两个值的类型不同,则===只会返回false。

我总是使用类型强制 (I always use type coercion)

Coerce a value to a type via Boolean, Number, String(), Object().

通过布尔,数字,字符串(),对象()将值强制转换为类型。

停止使用关键字this作为隐式参数 (Stop using the keyword this as an implicit parameter)

I don’t use the keyword this as an implicit parameter to keep object-oriented and functional mechanism separate.

我不使用关键字this作为隐式参数来使面向对象和功能机制保持分离。

我通过in和hasOwnProperty()函数中的关键字检查属性的存在 (I check for the existence of a property via the keyword in and hasOwnProperty() function)

It is safer than comparing a property with undefined or checking for truthiness.

比将undefined的属性进行比较或检查真实性要安全得多。

我总是容忍失败 (I always allow a fail fast)

Finally, if you can it’s best to fail fast and to not fail silently

最后,如果可以的话,最好快速失败,不要默默地失败

Useful, right?

有用吧?

翻译自: https://medium.com/javascript-in-plain-english/javascript-best-practices-with-my-experiences-6d16e371f669

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition (pdf + ePub) by Russ Ferguson and Christian Heilmann Publisher: Apress; 2nd Edition (July 2013) Language: English ISBN-10: 1430250925 ISBN-13: 978-1430250920 BOOK DESCRIPTION Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-speed with JavaScript development and add dynamic enhancements to web pages, right from the basics. As well as focusing on client-side JavaScript, you will also learn how to work with the Browser Object Model, the Document Object Model (DOM), how to use XML and JSON as well as communicate with service side scripts such as PHP. Find out how to: Construct good JavaScript syntax following modern coding practices Use JavaScript to communicate with the server and retrieve data Dynamically manipulate markup, validate forms and deal with images Debug applications using features inside the browser JavaScript is one of the most important technologies on the web. It provides the means to add dynamic functionality to your web pages and serves as the backbone of Ajax-style web development. Beginning JavaScript with DOM Scripting and Ajax will take you from being a JavaScript novice to work freely with this important technology – begin your JavaScript journey today! What you'll learn What functions, variables, events and objects are and how to use them. How build a site that will still work in the case that JavaScript is turned off. How to access and update part of the page using code. How to use JavaScript to communicate with the server and retrieve data. How to use JavaScript to for form validation and user feedback. How to use Third-Party Libraries like jQuery. Who this book is for Beginning JavaScript with DOM Scripting and Ajax is for the person who has a good grasp of HTML and CSS but wants to add JavaScript to their skillset. If you want to learn some basic programming concepts, have experience but need help updating your skills, or you’re coming from another language, Beginning JavaScript with DOM Scripting and Ajax can help. Table of Contents Chapter 1. Getting Started with JavaScript Chapter 2. Data and Decisions Chapter 3. From DHTML to DOM Scripting Chapter 4. HTML and JavaScript Chapter 5. Presentation and Behavior (CSS and Event Handling) Chapter 6. Common Uses of JavaScript: Images and Windows Chapter 7. JavaScript and User Interaction: Navigation and Forms Chapter 8. Back-End Interaction with Ajax and Node.js Chapter 9. Data Validation Techniques Chapter 10. Modern JavaScript Case Study: A Dynamic Gallery Chapter 11. Using Third-Party JavaScript Appendix A. Debugging JavaScript
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值