我弄得xmind的更加的清晰全面,還有很多備註,想要思維導圖的直接評論發郵鞋就好,我會發給你。
string
number
boolean
null
undefined
1
2
3
4
5
6
|
var
foo
=
1
,
bar
=
foo
;
bar
=
9
;
console
.
log
(
foo
,
bar
)
;
// => 1, 9
|
object
array
function
1
2
3
4
5
6
|
var
foo
=
[
1
,
2
]
,
bar
=
foo
;
bar
[
0
]
=
9
;
console
.
log
(
foo
[
0
]
,
bar
[
0
]
)
;
// => 9, 9
|
1
2
3
4
5
|
// bad
var
item
=
new
Object
(
)
;
// good
var
item
=
{
}
;
|
1
2
3
4
5
6
7
8
9
10
11
|
// bad
var
superman
=
{
default
:
{
clark
:
&
#039;kent' },
private
:
true
}
;
// good
var
superman
=
{
defaults
:
{
clark
:
&
#039;kent' },
hidden
:
true
}
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// bad
var
superman
=
{
class
:
&
#039;alien'
}
;
// bad
var
superman
=
{
klass
:
&
#039;alien'
}
;
// good
var
superman
=
{
type
:
&
#039;alien'
}
;
|
1
2
3
4
5
|
// bad
var
items
=
new
Array
(
)
;
// good
var
items
=
[
]
;
|
1
2
3
4
5
6
7
|
var
someStack
=
[
]
;
// bad
someStack
[
someStack
.
length
]
=
&
#039;abracadabra';
// good
someStack
.
push
(
&
#039;abracadabra');
|
1
2
3
4
5
6
7
8
9
10
11
|
var
len
=
items
.
length
,
itemsCopy
=
[
]
,
i
;
// bad
for
(
i
=
0
;
i
&
lt
;
len
;
i
++
)
{
itemsCopy
[
i
]
=
items
[
i
]
;
}
// good
itemsCopy
=
items
.
slice
(
)
;
|
1
2
3
4
|
function
trigger
(
)
{
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
)
;
.
.
.
}
|
1
2
3
4
5
6
7
8
9
10
11
|
// bad
var
name
=
&
quot
;
Bob
Parr
&
quot
;
;
// good
var
name
=
&
#039;Bob Parr';
// bad
var
fullName
=
&
quot
;
Bob
&
quot
;
+
this
.
lastName
;
// good
var
fullName
=
&
#039;Bob ' + this.lastName;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// bad
var
errorMessage
=
&
#039;This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
// bad
var
errorMessage
=
&
#039;This is a super long error that was thrown because
of
Batman
.
When
you
stop
to
think
about
how
Batman
had
anything
to
do
with
this
,
you
would
get
nowhere
fast
.
&
#039;;
// good
var
errorMessage
=
&
#039;This is a super long error that was thrown because ' +
&
#039;of Batman. When you stop to think about how Batman had anything to do ' +
&
#039;with this, you would get nowhere fast.';
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
var
items
,
messages
,
length
,
i
;
messages
=
[
{
state
:
&
#039;success',
message
:
&
#039;This one worked.'
}
,
{
state
:
&
#039;success',
message
:
&
#039;This one worked as well.'
}
,
{
state
:
&
#039;error',
message
:
&
#039;This one did not work.'
}
]
;
length
=
messages
.
length
;
// bad
function
inbox
(
messages
)
{
items
=
&
#039;<ul>';
for
(
i
=
0
;
i
&
lt
;
length
;
i
++
)
{
items
+=
&
#039;<li>' + messages[i].message + '</li>';
}
return
items
+
&
#039;</ul>';
}
// good
function
inbox
(
messages
)
{
items
=
[
]
;
for
(
i
=
0
;
i
&
lt
;
length
;
i
++
)
{
items
[
i
]
=
messages
[
i
]
.
message
;
}
return
&
#039;<ul><li>' + items.join('</li><li>') + '</li></ul>';
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// anonymous function expression
var
anonymous
=
function
(
)
{
return
true
;
}
;
// named function expression
var
named
=
function
named
(
)
{
return
true
;
}
;
// immediately-invoked function expression (IIFE)
(
function
(
)
{
console
.
log
(
&
#039;Welcome to the Internet. Please follow me.');
}
)
(
)
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// bad
if
(
currentUser
)
{
function
test
(
)
{
console
.
log
(
&
#039;Nope.');
}
}
// good
var
test
;
if
(
currentUser
)
{
test
=
function
test
(
)
{
console
.
log
(
&
#039;Yup.');
}
;
}
|
1
2
3
4
5
6
7
8
9
|
// bad
function
nope
(
name
,
options
,
arguments
)
{
// ...stuff...
}
// good
function
yup
(
name
,
options
,
args
)
{
// ...stuff...
}
|
1
2
3
4
5
6
7
8
9
10
|
var
luke
=
{
jedi
:
true
,
age
:
28
}
;
// bad
var
isJedi
=
luke
[
&
#039;jedi'];
// good
var
isJedi
=
luke
.
jedi
;
|
1
2
3
4
5
6
7
8
9
10
|
var
luke
=
{
jedi
:
true
,
age
:
28
}
;
function
getProp
(
prop
)
{
return
luke
[
prop
]
;
}
var
isJedi
=
getProp
(
&
#039;jedi');
|
1
2
3
4
5
|
// bad
superPower
=
new
SuperPower
(
)
;
// good
var
superPower
=
new
SuperPower
(
)
;
|
1
2
3
4
5
6
7
8
9
|
// bad
var
items
=
getItems
(
)
;
var
goSportsTeam
=
true
;
var
dragonball
=
&
#039;z';
// good
var
items
=
getItems
(
)
,
goSportsTeam
=
true
,
dragonball
=
&
#039;z';
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// bad
var
i
,
len
,
dragonball
,
items
=
getItems
(
)
,
goSportsTeam
=
true
;
// bad
var
i
,
items
=
getItems
(
)
,
dragonball
,
goSportsTeam
=
true
,
len
;
// good
var
items
=
getItems
(
)
,
goSportsTeam
=
true
,
dragonball
,
length
,
i
;
·
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
// bad
function
(
)
{
test
(
)
;
console
.
log
(
&
#039;doing stuff..');
//..other stuff..
var
name
=
getName
(
)
;
if
(
name
===
&
#039;test') {
return
false
;
}
return
name
;
}
// good
function
(
)
{
var
name
=
getName
(
)
;
test
(
)
;
console
.
log
(
&
#039;doing stuff..');
//..other stuff..
if
(
name
===
&
#039;test') {
return
false
;
}
return
name
;
}
// bad
function
(
)
{
var
name
=
getName
(
)
;
if
(
!
arguments
.
length
)
{
return
false
;
}
return
true
;
}
// good
function
(
)
{
if
(
!
arguments
.
length
)
{
return
false
;
}
var
name
=
getName
(
)
;
return
true
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// we know this wouldn't work (assuming there
// is no notDefined global variable)
function
example
(
)
{
console
.
log
(
notDefined
)
;
// => throws a ReferenceError
}
// creating a variable declaration after you
// reference the variable will work due to
// variable hoisting. Note: the assignment
// value of `true` is not hoisted.
function
example
(
)
{
console
.
log
(
declaredButNotAssigned
)
;
// => undefined
var
declaredButNotAssigned
=
true
;
}
// The interpreter is hoisting the variable
// declaration to the top of the scope.
// Which means our example could be rewritten as:
function
example
(
)
{
var
declaredButNotAssigned
;
console
.
log
(
declaredButNotAssigned
)
;
// => undefined
declaredButNotAssigned
=
true
;
}
|
1
2
3
4
5
6
7
8
9
|
·
function
example
(
)
{
console
.
log
(
anonymous
)
;
// => undefined
anonymous
(
)
;
// => TypeError anonymous is not a function
var
anonymous
=
function
(
)
{
console
.
log
(
&
#039;anonymous function expression');
}
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
function
example
(
)
{
console
.
log
(
named
)
;
// => undefined
named
(
)
;
// => TypeError named is not a function
superPower
(
)
;
// => ReferenceError superPower is not defined
var
named
=
function
superPower
(
)
{
console
.
log
(
&
#039;Flying');
}
;
}
// the same is true when the function name
// is the same as the variable name.
function
example
(
)
{
console
.
log
(
named
)
;
// => undefined
named
(
)
;
// => TypeError named is not a function
var
named
=
function
named
(
)
{
console
.
log
(
&
#039;named');
}
}
|
1
2
3
4
5
6
7
|
function
example
(
)
{
superPower
(
)
;
// => Flying
function
superPower
(
)
{
console
.
log
(
&
#039;Flying');
}
}
|
''
返回 false 其他返回true
1
2
3
4
|
if
(
[
0
]
)
{
// true
// An array is an object, objects evaluate to true
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// bad
if
(
name
!==
&
#039;') {
// ...stuff...
}
// good
if
(
name
)
{
// ...stuff...
}
// bad
if
(
collection
.
length
&
gt
;
0
)
{
// ...stuff...
}
// good
if
(
collection
.
length
)
{
// ...stuff...
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// bad
if
(
test
)
return
false
;
// good
if
(
test
)
return
false
;
// good
if
(
test
)
{
return
false
;
}
// bad
function
(
)
{
return
false
;
}
// good
function
(
)
{
return
false
;
}
|
/** ... */
,包括描述,指定類型、所有參數的值和返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// bad
// make() returns a new element
// based on the passed in tag name
//
// @param <String> tag
// @return <Element> element
function
make
(
tag
)
{
// ...stuff...
return
element
;
}
// good
/**
* make() returns a new element
* based on the passed in tag name
*
* @param <String> tag
* @return <Element> element
*/
function
make
(
tag
)
{
// ...stuff...
return
element
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// bad
var
active
=
true
;
// is current tab
// good
// is current tab
var
active
=
true
;
// bad
function
getType
(
)
{
console
.
log
(
&
#039;fetching type...');
// set the default type to 'no type'
var
type
=
this
.
_type
||
&
#039;no type';
return
type
;
}
// good
function
getType
(
)
{
console
.
log
(
&
#039;fetching type...');
// set the default type to 'no type'
var
type
=
this
.
_type
||
&
#039;no type';
return
type
;
}
|
// FIXME:
來標記問題
1
2
3
4
5
6
7
|
function
Calculator
(
)
{
// FIXME: shouldn't use a global here
total
=
0
;
return
this
;
}
|
1
2
3
4
5
6
7
|
function
Calculator
(
)
{
// TODO: total should be configurable by an options param
this
.
total
=
0
;
return
this
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// bad
function
(
)
{
∙∙∙∙
var
name
;
}
// bad
function
(
)
{
∙
var
name
;
}
// good
function
(
)
{
∙∙
var
name
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// bad
function
test
(
)
{
console
.
log
(
&
#039;test');
}
// good
function
test
(
)
{
console
.
log
(
&
#039;test');
}
// bad
dog
.
set
(
&
#039;attr',{
age
:
&
#039;1 year',
breed
:
&
#039;Bernese Mountain Dog'
}
)
;
// good
dog
.
set
(
&
#039;attr', {
age
:
&
#039;1 year',
breed
:
&
#039;Bernese Mountain Dog'
}
)
;
|
1
2
3
4
5
|
// bad
var
x
=
y
+
5
;
// good
var
x
=
y
+
5
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// bad
(
function
(
global
)
{
// ...stuff...
}
)
(
this
)
;
// bad
(
function
(
global
)
{
// ...stuff...
}
)
(
this
)
;
// good
(
function
(
global
)
{
// ...stuff...
}
)
(
this
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// bad
$
(
&
#039;#items').find('.selected').highlight().end().find('.open').updateCount();
// good
$
(
&
#039;#items')
.
find
(
&
#039;.selected')
.
highlight
(
)
.
end
(
)
.
find
(
&
#039;.open')
.
updateCount
(
)
;
// bad
var
leds
=
stage
.
selectAll
(
&
#039;.led').data(data).enter().append('svg:svg').class('led', true)
.
attr
(
&
#039;width', (radius + margin) * 2).append('svg:g')
.
attr
(
&
#039;transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.
call
(
tron
.
led
)
;
// good
var
leds
=
stage
.
selectAll
(
&
#039;.led')
.
data
(
data
)
.
enter
(
)
.
append
(
&
#039;svg:svg')
.
class
(
&
#039;led', true)
.
attr
(
&
#039;width', (radius + margin) * 2)
.
append
(
&
#039;svg:g')
.
attr
(
&
#039;transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.
call
(
tron
.
led
)
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// bad
var
once
,
upon
,
aTime
;
// good
var
once
,
upon
,
aTime
;
// bad
var
hero
=
{
firstName
:
&
#039;Bob'
,
lastName
:
&
#039;Parr'
,
heroName
:
&
#039;Mr. Incredible'
,
superPower
:
&
#039;strength'
}
;
// good
var
hero
=
{
firstName
:
&
#039;Bob',
lastName
:
&
#039;Parr',
heroName
:
&
#039;Mr. Incredible',
superPower
:
&
#039;strength'
}
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// bad
var
hero
=
{
firstName
:
&
#039;Kevin',
lastName
:
&
#039;Flynn',
}
;
var
heroes
=
[
&
#039;Batman',
&
#039;Superman',
]
;
// good
var
hero
=
{
firstName
:
&
#039;Kevin',
lastName
:
&
#039;Flynn'
}
;
var
heroes
=
[
&
#039;Batman',
&
#039;Superman'
]
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// bad
(
function
(
)
{
var
name
=
&
#039;Skywalker'
return
name
}
)
(
)
// good
(
function
(
)
{
var
name
=
&
#039;Skywalker';
return
name
;
}
)
(
)
;
// good (guards against the function becoming an argument when two files with IIFEs are concatenated)
;
(
function
(
)
{
var
name
=
&
#039;Skywalker';
return
name
;
}
)
(
)
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// => this.reviewScore = 9;
// bad
var
totalScore
=
this
.
reviewScore
+
&
#039;';
// good
var
totalScore
=
&
#039;' + this.reviewScore;
// bad
var
totalScore
=
&
#039;' + this.reviewScore + ' total score';
// good
var
totalScore
=
this
.
reviewScore
+
&
#039; total score';
|
parseInt
對
Numbers
型進行強制轉型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
var
inputValue
=
&
#039;4';
// bad
var
val
=
new
Number
(
inputValue
)
;
// bad
var
val
=
+
inputValue
;
// bad
var
val
=
inputValue
&
gt
;
&
gt
;
0
;
// bad
var
val
=
parseInt
(
inputValue
)
;
// good
var
val
=
Number
(
inputValue
)
;
// good
var
val
=
parseInt
(
inputValue
,
10
)
;
|
parseInt
是你的瓶頸,出於性能考慮你需要使用
位移,那麼留下你的註釋來解釋原因。
1
2
3
4
5
6
7
|
// good
/**
* parseInt was the reason my code was slow.
* Bitshifting the String to coerce it to a
* Number made it a lot faster.
*/
var
val
=
inputValue
&
gt
;
&
gt
;
0
;
|
1
2
3
|
2147483647
&
gt
;
&
gt
;
0
//=> 2147483647
2147483648
&
gt
;
&
gt
;
0
//=> -2147483648
2147483649
&
gt
;
&
gt
;
0
//=> -2147483647
|
1
2
3
4
5
6
7
8
9
10
|
var
age
=
0
;
// bad
var
hasAge
=
new
Boolean
(
age
)
;
// good
var
hasAge
=
Boolean
(
age
)
;
// good
var
hasAge
=
!
!
age
;
|
1
2
3
4
5
6
7
8
9
|
// bad
function
q
(
)
{
// ...stuff...
}
// good
function
query
(
)
{
// ..stuff..
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// bad
var
OBJEcttsssss
=
{
}
;
var
this_is_my_object
=
{
}
;
function
c
(
)
{
}
var
u
=
new
user
(
{
name
:
&
#039;Bob Parr'
}
)
;
// good
var
thisIsMyObject
=
{
}
;
function
thisIsMyFunction
(
)
{
}
var
user
=
new
User
(
{
name
:
&
#039;Bob Parr'
}
)
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// bad
function
user
(
options
)
{
this
.
name
=
options
.
name
;
}
var
bad
=
new
user
(
{
name
:
&
#039;nope'
}
)
;
// good
function
User
(
options
)
{
this
.
name
=
options
.
name
;
}
var
good
=
new
User
(
{
name
:
&
#039;yup'
}
)
;
|
1
2
3
4
5
6
|
// bad
this
.
__firstName__
=
&
#039;Panda';
this
.
firstName_
=
&
#039;Panda';
// good
this
.
_firstName
=
&
#039;Panda';
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// bad
function
(
)
{
var
self
=
this
;
return
function
(
)
{
console
.
log
(
self
)
;
}
;
}
// bad
function
(
)
{
var
that
=
this
;
return
function
(
)
{
console
.
log
(
that
)
;
}
;
}
// good
function
(
)
{
var
_this
=
this
;
return
function
(
)
{
console
.
log
(
_this
)
;
}
;
}
|
1
2
3
4
5
6
7
8
9
|
// bad
var
log
=
function
(
msg
)
{
console
.
log
(
msg
)
;
}
;
// good
var
log
=
function
log
(
msg
)
{
console
.
log
(
msg
)
;
}
;
|
1
2
3
4
5
6
7
8
9
10
11
|
// bad
dragon
.
age
(
)
;
// good
dragon
.
getAge
(
)
;
// bad
dragon
.
age
(
25
)
;
// good
dragon
.
setAge
(
25
)
;
|
如果這個屬性是boolean,使用isVal() 或者 hasVal()
1
2
3
4
5
6
7
8
9
|
// bad
if
(
!
dragon
.
age
(
)
)
{
return
false
;
}
// good
if
(
!
dragon
.
hasAge
(
)
)
{
return
false
;
}
|
.也可以使用get() 和 set()函數來創建,但是必須一致。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
function
Jedi
(
options
)
{
options
||
(
options
=
{
}
)
;
var
lightsaber
=
options
.
lightsaber
||
&
#039;blue';
this
.
set
(
&
#039;lightsaber', lightsaber);
}
Jedi
.
prototype
.
set
=
function
(
key
,
val
)
{
this
[
key
]
=
val
;
}
;
Jedi
.
prototype
.
get
=
function
(
key
)
{
return
this
[
key
]
;
}
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
function
Jedi
(
)
{
console
.
log
(
&
#039;new jedi');
}
// bad
Jedi
.
prototype
=
{
fight
:
function
fight
(
)
{
console
.
log
(
&
#039;fighting');
}
,
block
:
function
block
(
)
{
console
.
log
(
&
#039;blocking');
}
}
;
// good
Jedi
.
prototype
.
fight
=
function
fight
(
)
{
console
.
log
(
&
#039;fighting');
}
;
Jedi
.
prototype
.
block
=
function
block
(
)
{
console
.
log
(
&
#039;blocking');
}
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// bad
Jedi
.
prototype
.
jump
=
function
(
)
{
this
.
jumping
=
true
;
return
true
;
}
;
Jedi
.
prototype
.
setHeight
=
function
(
height
)
{
this
.
height
=
height
;
}
;
var
luke
=
new
|