51 lines
1.2 KiB
Plaintext
51 lines
1.2 KiB
Plaintext
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
|
|
|
|
----
|
|
function MyNonClass(initializerArgs = []) {
|
|
this._values = [...initializerArgs];
|
|
}
|
|
|
|
MyNonClass.prototype.doSomething = function () { // Noncompliant
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
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[]
|