rspec/rules/S1462/flex/rule.adoc

51 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
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);
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
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);
}
----