55 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Originally JavaScript didn't support ``++class++``es, and class-like behavior had to be kludged using things like ``++prototype++`` assignments for "class" functions. Fortunately, ECMAScript 2015 added classes, so any lingering ``++prototype++`` uses should be converted to true ``++class++``es. The new syntax is more expressive and clearer, especially to those with experience in other languages.
Specifically, with ES2015, you should simply declare a ``++class++`` and define its methods inside the class declaration.
=== 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
----
function MyNonClass(initializerArgs = []) {
this._values = [...initializerArgs];
}
MyNonClass.prototype.doSomething = function () { // Noncompliant
// ...
}
----
=== 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
----
class MyClass {
constructor(initializerArgs = []) {
this._values = [...initializerArgs];
}
doSomething() {
//...
}
}
----
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)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]