58 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-06-30 12:47:33 +02:00
include::../description.adoc[]
== Noncompliant Code Example
----
function doSomething(a, b) { // "a" is unused
return compute(b);
}
----
== Compliant Solution
----
function doSomething(b) {
return compute(b);
}
----
== Exceptions
When writing function callbacks, some arguments might be required as part of the function signature, but not actually needed by the callback code. For instance, JQuery has the 'each' helper to iterate over arrays or objects, but using the counter 'i' should remain optional:
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
----
$(["first", "last"]).each(function (i, value) {
computeSomethingWithValue(value);
});
----
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
So only unused arguments listed at the end of the argument list will be flagged with issues because they could be omitted from the function signature. Unused arguments which are followed by an argument that _is_ used will be ignored.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
Examples :
2020-06-30 12:47:33 +02:00
----
var myFirsCallBackFunction = function (p1, p2, p3, p4) { // p2 is ignored, but p4 is reported
return p1 + p3; }
var mySecondCallBackFunction = function (p1, p2, p3, p4) { // p1, p2 and p3 are ignored
return p4; }
var myThirdCallBackFunction = function (p1, p2, p3, p4) { // p1 is ignored but p3 and p4 are reported
return p2; }
----
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
Further, when ``++arguments++`` is used in the function body, no parameter is reported as unused.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
----
function doSomething(a, b, c) {
compute(arguments);
}
----
include::../see.adoc[]