-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathBadTypeof.qhelp
55 lines (42 loc) · 1.89 KB
/
BadTypeof.qhelp
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
54
55
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
In ECMAScript 5, the <code>typeof</code> operator returns one of the following six type tags:
<code>"undefined"</code>, <code>"boolean"</code>, <code>"number"</code>, <code>"string"</code>,
<code>"object"</code>, <code>"function"</code>. In ECMAScript 2015, it may additionally return
<code>"symbol"</code>, while on older versions of Internet Explorer it may return
<code>"unknown"</code> or <code>"date"</code> in certain situations. Comparing it against
any other string literal is therefore useless.
</p>
</overview>
<recommendation>
<p>
Carefully examine the comparison in question. If the type tag is simply misspelled, correct it.
In some cases, the type tag returned by <code>typeof</code> is not sufficiently precise, so
you may have to use other type test functions.
</p>
</recommendation>
<example>
<p>
The following code snippet tries to determine whether <code>a</code> is an array:
</p>
<sample src="examples/BadTypeof.js" />
<p>
Note that <code>typeof</code> is not precise enough to distinguish arrays from other objects,
since it returns the type tag <code>"object"</code> for both. ECMAScript 5-compatible platforms
provide a library function <code>Array.isArray</code> that can be used instead:
</p>
<sample src="examples/BadTypeofGood.js" />
<p>
On older platforms, you can use the technique explained on the
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Compatibility">Mozilla Developer Network</a>.
</p>
</example>
<references>
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a>.</li>
<li>Mozilla Developer Network: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray">Array.isArray()</a>.</li>
</references>
</qhelp>