66 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
``++undefined++`` is the value you get for variables and properties which have not yet been created. Use the same value to reset an existing variable and you lose the ability to distinguish between a variable that exists but has no value and a variable that does not yet exist. Instead, ``++null++`` should be used, allowing you to tell the difference between a property that has been reset and one that was never created.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
var myObject = {};
// ...
myObject.fname = undefined; // Noncompliant
// ...
if (myObject.lname == undefined) {
// property not yet created
}
if (myObject.fname == undefined) {
// no real way of knowing the true state of myObject.fname
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
var myObject = {};
// ...
myObject.fname = null;
// ...
if (myObject.lname == undefined) {
// property not yet created
}
if (myObject.fname == undefined) {
// no real way of knowing the true state of myObject.fname
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use ``++null++`` instead.
'''
== Comments And Links
(visible only on this page)
=== on 10 Oct 2014, 17:26:20 Ann Campbell wrote:
Assigned to you for review [~linda.martin]
=== on 19 May 2015, 15:50:53 Linda Martin wrote:
Reviewed.
endif::env-github,rspecator-view[]