61 lines
2.0 KiB
Plaintext
61 lines
2.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Creating an object without assigning it to a variable or using it in any function means the object is essentially created for no reason and may be dropped immediately without being used. Most of the time, this is due to a missing piece of code and could lead to an unexpected behavior.
|
|
|
|
If it's intended because the constructor has side effects, that side effect should be moved into a separate method and called directly. This can help to improve the performance and readability of the code.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
new MyConstructor(); // Noncompliant: object may be dropped
|
|
----
|
|
|
|
Determine if the objects are necessary for the code to function correctly. If they are not required, remove them from the code. Otherwise, assign them to a variable for later use.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let something = new MyConstructor();
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
* Creating new objects inside a ``++try++`` block is ignored.
|
|
|
|
[source,javascript]
|
|
----
|
|
try {
|
|
new MyConstructor();
|
|
} catch (e) {
|
|
/* ... */
|
|
}
|
|
----
|
|
* Known constructors with side effects like `Notification` or `Vue` are also ignored.
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor[``Object.prototype.constructor``]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor[constructor]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new[`new` operator]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Either remove this useless object instantiation of "XXXX" or use it
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 27 May 2015, 11:57:01 Ann Campbell wrote:
|
|
Looks good [~freddy.mallet]. I forgot that moving this to a sub-task wouldn't clear out the extra fields. Thanks for taking care of that.
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|