Last active
December 14, 2015 17:59
-
-
Save kirushik/5125778 to your computer and use it in GitHub Desktop.
Нашёл на просторах интернетов (http://stackoverflow.com/a/14155541/181800) вот этот клёвый хак. При попытке создания лишних инстансов Direction сваливается с ошибкой, что в общем-то ожидаемое поведение в отсутствие приватных конструкторов.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
class Direction { | |
private static const CREATE:Object = {}; | |
/* | |
* The actual enumeration values. Note that the sorting order doesn't matter. | |
*/ | |
public static const NONE:Direction = new Direction(CREATE); | |
public static const UP:Direction = new Direction(CREATE); | |
public static const DOWN:Direction = new Direction(CREATE); | |
public static const LEFT:Direction = new Direction(CREATE); | |
public static const RIGHT:Direction = new Direction(CREATE); | |
/* Constructor */ | |
public function Direction(permission:Object) { | |
if (permission !== CREATE){ | |
throw new Error("Enum cannot be instantiated from outside"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment