rspec/rules/S1462/flex/rule.adoc
2022-02-04 16:28:24 +00:00

68 lines
1.4 KiB
Plaintext

Using plain string event names in even listeners is an anti-pattern; if the event is renamed, the application can start behaving unexpectedly. A constant variable should be used instead.
== Noncompliant Code Example
[source,flex]
----
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public function ChildSprite()
{
...
addEventListener("CustomEvent", clickHandler); // Noncompliant
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}
----
== Compliant Solution
[source,flex]
----
import flash.display.Sprite;
import flash.events.MouseEvent;
class ChildSprite extends Sprite
{
public const CUSTOM_EVENT:String = "CustomEvent";
public function ChildSprite()
{
...
addEventListener(CUSTOM_EVENT, clickHandler);
}
}
function clickHandler(event:CustomEvent):void
{
trace("clickHandler detected an event of type: " + event.type);
trace("the this keyword refers to: " + this);
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]