Taking notes while reading books, this warehouse mainly refers to the book "Writing Maintainable JavaScript" to summarize its own study notes. It’s just your own summary, which is different from the book. If you think it’s good, you can buy this book and study it yourself. The book also involves the difference in JSLint, JSHint. Where to get the code from, the code styles of various organizations or companies, and compare and summarize. The views on certain writing styles are sometimes ambiguous. My own summary does not have these things, and I prefer how to write them myself.
Finally, it is not easy for me to write this article. If it feels good, please click Star
, thank you everyone.
Basic formatting
Comments
Statements and expressions
Variables, functions, and operators
if ( wl && wl . length ) {
for ( i = 0 , 1 = wl . length ; i < 1 ; ++ i ) {
p = wl [ i ] ;
type = Y . Lang . type ( r [ p ] ) ;
if ( s . hasOwnProperty ( p ) ) { if ( merge && type == 'object' ) {
Y . mix ( r ( p ] , s { p ] ) ;
} else if ( ov || ! ( p in r ) ) {
r [ p ] = s [ p ] ;
}
}
}
}
if ( wl && wl . length ) {
for ( i = 0 , 1 = wl . length ; i < 1 ; ++ i ) {
p = wl [ i ] ;
type = Y . Lang . type ( r [ p ] ) ;
if ( s . hasOwnProperty ( p ) ) {
if ( merge && type == 'object' ) {
Y . mix ( r [ p ] , s [ p ] ) ;
} else if ( ov || ! ( p in r ) ) {
r [ p ] = s [ p ] ;
}
}
}
}
There are two opinions
Both of the above methods are OK, but do not use the two methods in a mixed manner.
Back to top
// 原始代码
function getData ( ) {
return
{
title : "Maintainable JavaScript" ,
author : "Nicholas C. Zakas"
}
}
// 分析器会将它理解成
function getData ( ) {
return ;
{
title : "Maintainable JavaScript" ,
author : "Nicholas C. Zakas"
} ;
}
In the above example, the function will return an undefined, which is not the result we want. Although {
can be put after return
, the analysis's judgment is relatively complicated and cannot guarantee that we can take into account all the situations.
All we should be more inclined to use them than to omit them.
The above is the view of the author of the book. I do see a style that no semicolon is added at the end. This writing method requires that the semicolon be added in front of the sentence when it starts with [
, (
, `.
Moreover, those who recommend this practice believe that it is not easy to make mistakes without adding a semicolon. Errors like the above will not be ignored until the online stage. When you create a method and do not get the results you want, they should be discovered immediately during development.
Even if the specification with a semicolon is used, it will sometimes be missed due to accidental care (there is no problem with the detection tool)
Finally, the third-party code used is not necessarily a semicolon-plus style. It is possible that you still have to add a semicolon in the first position.
// 这些语句与上一句没有分号的语句连接的话会直接报错,所以必须加分号
; [ 1 , 2 , 3 ] . foreach ( function ( ) {
// 处理内容
} )
; `shenxf ${ nickname } ` . length
// 很多代码都习惯于在圆括号之前加一个分号,这是由于之前引入的代码可能有漏了分号的情况,
// 为了以防万一,都会有类似于下面的代码。
; ( function ( ) {
// 处理内容。
} ) ( )
[
, (
,`.Back to top
Back to top
// 好的做法:在运算符后面换行。 第二行追加二个缩进
callAFunction ( document , element , window , "some string value" , true , 123 ,
navigator ) ;
// 不好的做法第二行只有一个缩进
callAFunction ( document , element , window , "some string value" , true , 123 ,
navigator ) ;
// 不好的做法运算符之前换行了
callAFunction ( document , element , window , "some string value" , true , 123
, navigator ) ;
if ( isLeapYear && isFebruary && day && day == 29 && itsYourBirthdady &&
noPlans ) {
waitAnotherFourYears ( ) ;
}
var result = something + anotherThing + yetAnotherThing + someThingElse +
anotherSomethingElse ;
Back to top
if ( wl && wl . length ) {
for ( i = 0 , 1 = wl . length ; i < 1 ; ++ i ) {
p = wl [ i ] ;
type = Y . Lang . type ( r [ p ] ) ;
if ( s . hasOwnProperty ( p ) ) {
if ( merge && type == 'object' ) {
Y . mix ( r [ p ] , s [ p ] ) ;
} else if ( ov || ! ( p in r ) ) {
r [ p ] = s [ p ] ;
}
}
}
}
if ( wl && wl . length ) {
for ( i = 0 , 1 = wl . length ; i < 1 ; ++ i ) {
p = wl [ i ] ;
type = Y . Lang . type ( r [ p ] ) ;
if ( s . hasOwnProperty ( p ) ) {
if ( merge && type == 'object' ) {
Y . mix ( r [ p ] , s [ p ] ) ;
} else if ( ov || ! ( p in r ) ) {
r [ p ] = s [ p ] ;
}
}
}
}
Back to top
Back to top
// 好的写法
var count = 10 ;
var myName = "shenxf" ;
var found = true ;
// 不好的写法: 变量看起来像函数
var getCount = 10 ;
var isFound = true ;
// 好的写法
function getName ( ) {
return myName ;
}
// 不好的写法
function theName ( ) {
return myName ;
}
The naming should be as short and refined as possible. For example, count, length and size can be seen as numeric types, name, title, and message can be seen as string types.
i, j, k are usually used in loop processing.
Avoid writing meaningless naming.
For the naming of the method, the first word should be a verb. Here are some common conventions
verb | meaning |
---|---|
can | The function returns a boolean value |
has | The function returns a boolean value |
is | The function returns a boolean value |
get | The function returns a non-boolean value |
set | Functions are used to save a value |
if ( isEnabled ( ) ) {
setName ( "shenxf" ) ;
}
if ( getName ( ) === "shenxf" ) {
doSomething ( ) ;
}
Back to top
var MAX_COUNT = 10 ;
var URL = "http://shenxf.top" ;
if ( count < MAX_COUNT ) {
doSomething ( ) ;
}
Back to top
// 好的做法
function Person ( name ) {
this . name = name ;
}
Person . prototype . sayName = function ( ) {
alert ( this . name ) ;
}
var me = new Person ( "shenxf" ) ;
var me = Person ( "shenxf" ) ; // 这里缺少了new
var you = getPerson ( "xx" ) ;
Back to top
Back to top
// 下面都是合法的javascript代码
var name = "shenxf says, "Hi."" ;
var name = 'shenxf says, "Hi."' ;
The authors of the book often use Java, which uses double quotes to represent strings. Therefore, the author recommends using double quotes because it can be consistent with the Java language.
I personally recommend using single quotes. Many of the encoding specification plugins I have used before declare variables in single quotes. I'm used to it, so I recommend using single quotes. No matter which one is used, I think it's enough to keep your code style consistent.
When strings are broken, operators should be used to connect.
// 不好的写法
var longString = "Here's the story, of a man
named Brady." ;
// 好的写法
var longString = "Here's the story, of a man" +
"named Brady." ;
Back to top
// 整数
var count = 10 ;
// 小数
var price = 10.0 ;
var price = 10.00 ;
// 不推荐小数的写法:没有小数部分
var pricee = 10. ;
// 不推荐小数的写法:没有整数部分
var price = .1 ;
// 不推荐的写法:八进制写法已经被弃用
var num = 010 ;
// 十六进制写法
var num = 0xA2 ;
// 科学计数法
var num = 1e23 ;
10
, but it is actually 8
, so it is not recommended.Back to top
null is more special and often confused with undefined.
Scenarios where null should be used
Scenarios where null should not be used
example
// 好的用法
var person = null ;
// 好的用法
function getPerson ( ) {
if ( condition ) {
return new Person ( "shenxf" ) ;
} else {
return null ;
}
}
// 好的用法
var person = getPerson ( ) ;
if ( person !== null ) {
doSomething ( ) ;
}
// 不好的写法:用来和未初始化的变量进行比较
var person ;
if ( person != null ) {
doSomething ( ) ;
}
// 不好的写法:检测是否传入了参数
function doSomething ( arg1 , arg2 , arg3 , arg4 ) {
if ( arg4 != null ) {
doSomethingElse ( ) ;
}
}
null
is to understand it as a placeholderBack to top
null == undefined
is true
, however, the usage of these 2 values varies.undefined
, indicating that the variable is waiting to be assigned. // 不好的写法
var person ;
console . log ( person === undefined ) ; //true
undefined
.undefined
has many confusing things, such as typeof
// foo未被声明
var person ;
console . log ( typeof person ) ; // "undefined"
console . log ( typeof foo ) ; // "undefined"
undefined
, the result of typeof
can be undefined
. That is when the variable is not declared. // 好的做法
var person = null ;
console . log ( person === null ) ; // true
typeof null
value returns Object
, which can avoid undefined
and distinguish it.Back to top
// 不好的写法
var book = new Object ( ) ;
book . title = "shenxfsbook" ;
book . author = "shenxf" ;
// 好的写法
var book = {
title : "shenxfsbook" ,
author : "shenxf"
} ;
Back to top
// 不好的写法
var colors = new Array ( "red" , "green" , "blue" ) ;
var numbers = new Array ( 1 , 2 , 3 , 4 ) ;
// 好的写法
var colors = [ "red" , "green" , "blue" ] ;
var numbers = [ 1 , 2 , 3 , 4 ] ;
Back to top
// 这是一句单行注释
It is best to reserve a space behind the double slashes.
There are 3 ways to use single line comments
Single-line comments should not be used for multiple lines unless they are large pieces of code that are commented.
// 好的写法
if ( condition ) {
// 如果代码执行到这里,则表示通过了左右的安全性检查
allowed ( ) ;
}
// 不好的写法:注释之前没有空行
if ( condition ) {
// 如果代码执行到这里,则表示通过了左右的安全性检查
allowed ( ) ;
}
// 不好的写法:错误的缩进
if ( condition ) {
// 如果代码执行到这里,则表示通过了左右的安全性检查
allowed ( ) ;
}
// 好的写法
var result = something + somethingElse ; // somethingElse不应当取值为null
// 不好的写法:代码和注释之间没有间隔
var result = something + somethingElse ; // somethingElse不应当取值为null
// 好的写法
// if (condition) {
// dosomething();
// thenDoSomethingElse();
// }
// 不好的写法:这里应当用多行注释
// 接下来的这段代码非常难,那么,让我详细的解释下
// 这段代码首先判断条件是否为真
// 。。。。。。。
if ( condition ) {
// 如果代码执行到这里,则表示通过了左右的安全性检查
allowed ( ) ;
}
Back to top
/* 我的注释 */
/* 另一段注释
这个注释包含2行 */
/*
又是一段注释
这段注释同样包含2行
*/
/*
, and the last line is */
, and each line starts with *
and opens a space /*
* 另一段注释
* 这个注释包含2行
*/
// 好的写法
if ( condition ) {
/*
* 如果代码执行到这里
* 说明通过了所有安全性检查
*/
allowed ( ) ;
}
// 不好的写法:注释之前无空行
if ( condition ) {
/*
* 如果代码执行到这里
* 说明通过了所有安全性检查
*/
allowed ( ) ;
}
// 不好的写法:星号之后没有空格
if ( condition ) {
/*
*如果代码执行到这里
*说明通过了所有安全性检查
*/
allowed ( ) ;
}
// 不好的写法:错误的缩进
if ( condition ) {
/*
* 如果代码执行到这里
* 说明通过了所有安全性检查
*/
allowed ( ) ;
}
// 不好的写法:代码尾部不要用多行注释
var result = something + somethingElse ; /*somethingElse不应当取值为null*/
Back to top
// 不好的写法:注释并没有提供有价值的信息
// 初始化count
var count = 10 ;
// 好的写法
// 改变这个值可能会使它变成青蛙
var count = 10 ;
Back to top
// 好的写法
if ( mode ) {
/*
* 当 mode 为2时
* 用来执行原型合并的操作。。。。
* 。。。。
*/
if ( mode === 2 ) {
Y . mix ( receiver . prototype , supplier . prototype , overwrite ,
whitelis , 0 , merge ) ;
}
/*
* 根据模式的类型。。。
* 。。。。
*/
from = mode === 1 || mode === 3 ? supplier . prototype : supperlier ;
to = mode === 1 || mode === 4 ? receiver . prototype : receiver ;
/*
* .......
* ......
*/
if ( ! from || ! to ) {
from = supperlier ;
to = receiver ;
}
} else {
from = supplier ;
to = receiver ;
}
Back to top
while ( element && ( element = element [ asix ] ) ) { // 赋值操作
if ( ( all || element [ TAG_NAME ] ) &&
( ! fn || fn ( element ) ) ) {
return element ;
}
}
Back to top
var ret = false ;
if ( ! needle || ! element || ! needle [ NODE_TYPE ] || ! element [ NODE_TYPE ] ) {
ret = false ;
} else if ( element [ CONTAINS ] ) {
// 如果needle不是ELEMENT_NODE时,IE和Safari下会有错误
if ( Y . UA . opera || needle [ NODE_TYPE ] === 1 ) {
ret = element [ CONTAINS ] ( needle ) ;
} else {
ret = Y_DOM . _bruteContains ( element , needle ) ;
}
} else if ( element [ COMPARE_DOCUMENT_POSITION ] ) { // gecko
if ( element === needle || ! ! ( element [ COMPARE_DOCUMENT_POSITION ] ( needle ) & 16 ) ) {
ret = true
}
}
Back to top
/**
, followed by description information, where the @
symbol represents one or more properties. /**
返回一个对象,这个对象包含被提供对象的所有属性。
后一个对象的属性会覆盖前一个对象的属性。
传入一个单独对象。。。。
@method merge
@param {Object} 被合并的一个或多个对象
@param {Object} 一个新的合并后的对象
**/
Y . merge = function ( ) {
var args = arguments ,
i = 0 ,
len = args . length ,
result = { } ;
for ( ; i < len ; ++ i ) {
Y . mix ( result , args [ i ] , true ) ;
}
return result ;
} ;
Back to top
There are 2 styles
if ( condition ) {
doSomething ( ) ;
} else {
doSomethingElse ( ) ;
}
if ( condition )
{
doSomething ( ) ;
}
else
{
doSomethingElse ( ) ;
}
Back to top
There are three main styles
if ( condition ) {
doSomething ( ) ;
}
if ( condition ) {
doSomething ( ) ;
}
if ( condition ) {
doSomething ( ) ;
}
Back to top
switch
statements in JavaScript is different from that in other languages: switch statements can use any type of value, and any expression can be legally used in case clauses. But in other languages, raw values and constants must be used.Back to top
switch ( condition ) {
case "first" :
// 代码
break ;
case "second" :
// 代码
break ;
case "third" :
// 代码
break ;
default :
// 代码
}
What's unique
case
statement is indented to a level relative to the switch
keyword.case
statement.Another style
switch ( condition ) {
case "first" :
// 代码
break ;
case "second" :
// 代码
break ;
case "third" :
// 代码
break ;
default :
// 代码
}
The difference is that case
keyword remains left-aligned with switch
.
Both the author and I like the Java style.
Back to top
break
after case
, the following conditions will be executed continuously, which will become the original sin of many system bugs. switch ( condition ) {
// 明显的依次执行
case "first" :
case "second" :
// 代码
break ;
case "third" :
// 代码
/* fall through */
default :
// 代码
}
third
, it means that this is intentional. This is also reasonable.Back to top
default
is needed, and many people never omit default
, even though it does nothing. switch ( condition ) {
case "first" :
// 代码
break ;
case "second" :
// 代码
break ;
default :
// default中没有逻辑
}
switch ( condition ) {
case "first" :
// 代码
break ;
case "second" :
// 代码
break ;
// 没有 default
}
This indicates that there is no default behavior and saves bytes.
I prefer the first one, you can just write default:
no comments. This also clearly knows that there is no default behavior. When adding default behavior in the future, you can write one less line of code. Actually, both methods are OK, it depends on which one you like.
Back to top
var book = {
title : "shenxf javascrpt" ,
author : "shenxf"
} ;
var message = " The book is " ;
with ( book ) {
message += title ;
message += " by " + author ;
}
title
and author
appear, and it is also difficult to distinguish whether message
is a local variable or a property of book
. Programmatically speaking, JavaScript engines and compression tools cannot optimize this code, so they cannot guess the true meaning of the code.Back to top
var values = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ,
i , len ;
for ( i = 0 , len = values . length ; i < len ; i ++ ) {
process ( values [ i ] ) ;
}
break
and continue
statements in the for loop, and I believe everyone knows how to use them.continue
. It is better to use conditional statements than to use continue
, and the logic is clearer. var values = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ,
i , len ;
for ( i = 0 , len = values . length ; i < len ; i ++ ) {
if ( i !== 2 ) {
process ( values [ i ] ) ;
}
}
Back to top
hasOwnProperty()
method to filter out instance properties for the for-in loop. var prop ;
for ( prop in object ) {
if ( object . hasOwnProperty ( prop ) ) {
console . log ( "Property name is " + prop ) ;
console . log ( "Property value is " + object [ prop ] ) ;
}
}
hasOwnProperty
is added, but if you just want to find the prototype chain, please add comments. var prop ;
for ( prop in object ) { // 包含对原型链的遍历
console . log ( "Property name is " + prop ) ;
console . log ( "Property value is " + object [ prop ] ) ;
}
// 不好的用法
var values = [ 1 , 2 , 3 , 4 , 5 ] ,
i ;
for ( i in values ) {
process ( item [ i ] ) ;
}
Back to top
function doSomething ( ) {
var result = 10 + value ;
var value = 10 ;
return result ;
}
function doSomething ( ) {
var result ;
var value
result = 10 + value ;
value = 10 ;
return result ;
}
The result of this function is the NaN value.
The author tends to style, put all definitions in the beginning of the scope, separated by commas, which can save the bytes written in var
.
function doSomethingWithItems ( items ) {
var value = 10 ,
result = value + 10 ,
i ,
len ;
for ( i = 0 , len = items . length ; i < len ; i ++ ) {
doSomething ( items [ i ] ) ;
}
}
Back to top
// 不好的写法
doSomething ( ) ;
function doSomething ( ) {
alert ( 'Hello world!' ) ;
}
function doSomething ( ) {
alert ( 'Hello world!' ) ;
}
doSomething ( ) ;
function doSomethingWithItems ( items ) {
var i , len ,
value = 10 ,
result = value + 10 ;
function doSomething ( item ) {
// 代码逻辑
}
for ( i = 0 , len = items . length ; i < len ; i ++ ) {
doSomething ( items [ i ] ) ;
}
}
// 不好的写法
if ( condition ) {
function doSomething ( ) {
alert ( 'hi' ) ;
}
} else {
function doSomething ( ) {
alert ( 'Yo' ) ;
}
}
Back to top
// 好的写法
doSomething ( item ) ;
// 不好的写法:看起来像一个语句块
doSomething ( item ) ;
// 用来做对比的块语句
while ( item ) {
// 代码逻辑
}
Back to top
// 一般的写法
var doSomething = function ( ) {
// 函数体
} ;
// 不好的写法 : 被误解为函数的赋值,其实是被赋值为一个对象
var value = function ( ) {
// 函数体
return {
message : "Hi"
}
} ( ) ;
// 好的写法
var value = ( function ( ) {
// 函数体
return {
message : "Hi"
}
} ( ) ) ;
Back to top
use strict
// 不好的写法 - 全局严格模式
"use strict"
function doSomething ( ) {
// 代码
}
// 好的写法
function doSomething ( ) {
"use strict"
// 代码
}
// 好的写法
( function ( ) {
"use strict"
function doSomething ( ) {
// 代码
}
function doSomethingElse ( ) {
// 代码
}
} ) ( ) ;
Back to top
// 比较数字5和字符串5
console . log ( 5 == "5" ) ; // true
// 比较数字25和十六进制的字符串25
console . log ( 25 == "0x19" ) ; // true
When a cast occurs, the string will be converted to a number, similar to using Number()
conversion function. It converts hexadecimal numbers correctly. So the second expression is equal.
Comparison of boolean values and numbers
// 数字 1 和 true
console . log ( 1 == true ) ; // true
// 数字 0 和 false
console . log ( 0 == false ) ; // true
// 数字 2 和 true
console . log ( 2 == true ) ; // false
valueOf()
method will be called first to get the original type and then compared. If there is no valueOf()
, toString()
will be called. var object = {
toString : function ( ) {
return "0x19" ;
}
} ;
console . log ( object == 25 ) ; // true
null
and undefined
are equal. console . log ( null == undefined ) ; // true
==
and !=
. You should use ===
and !==
. They will not convert type conversion and return false
as long as the types are different. // 比较数字5和字符串5
console . log ( 5 == "5" ) ; // true
console . log ( 5 === "5" ) ; // false
// 比较数字25和十六进制的字符串25
console . log ( 25 == "0x19" ) ; // true
console . log ( 25 === "0x19" ) ; // false
// 数字 1 和 true
console . log ( 1 == true ) ; // true
console . log ( 1 === true ) ; // false
// 数字 0 和 false
console . log ( 0 == false ) ; // true
console . log ( 0 === false ) ; // false
// 数字 2 和 true
console . log ( 2 == true ) ; // false
console . log ( 2 === true ) ; // false
var object = {
toString : function ( ) {
return "0x19" ;
}
} ;
// 一个对象和25
console . log ( object == 25 ) ; // true
console . log ( object === 25 ) ; // false
// null和undefined
console . log ( null == undefined ) ; // true
console . log ( null === undefined ) ; // false
Back to top
Image recognition
ocr windows installation
You can also convert pdf to png file directly and then use ocr to grab text.
Crawl English tesseract xxx.png output_1 -l eng
Crawl Chinese tesseract xxx.png output_1 -l chi_sim