47 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
When a variable is assigned an ``++undefined++`` or ``++null++`` value, it has no properties. Trying to access properties of such a variable anyway results in a ``++TypeError++``, causing abrupt termination of the script if the error is not caught in a ``++catch++`` block. But instead of ``++catch++``-ing this condition, it is best to avoid it altogether.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
if (x === undefined) {
console.log(x.bar); // Noncompliant; TypeError will be thrown
2020-06-30 12:48:07 +02:00
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 15 Mar 2016, 09:45:14 Pierre-Yves Nicolas wrote:
Useful links:
* \https://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/
* \https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
We could make this rule also cover invalid function calls, e.g.:
----
var x = 42;
x(); // Noncompliant: TypeError will be thrown
----
However, there may be some overlap with RSPEC-2999.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]