RPGMaker MV ES6 Part 6: Symbols

Introduction

Symbols are a new part of the JavaScript specification; they are not a part of ES5. RPGMakerMV has support for symbols, which gives us one more feature we have access to. Now, let’s get into the details of what a symbol is.

Symbols

Symbols are unique immutable data types in JavaScript; this means that a symbol can’t be changed, and no two symbols are the same. Symbols also can’t be accessed by name, although, you can attach an optional description/name to them; you would need a reference to the symbol to use it again. Lastly, object properties with symbols as keys can’t be serialized into a JSON file. Well then, how are symbols useful to us?

Symbols are useful to us because we can create unique keys that are more secure than ID numbers, or string keys while the program is running.

Symbol Methods

The symbol data type does have two special methods attached to it; these two methods allow us to maintain a list of symbols while the program is running. This list is the global symbol registry, and it provides us with global symbols.

Symbol.For()

This method returns a symbol with the specified key or creates a new symbol if it doesn’t exist yet on the global symbol registry. Any symbol created with this method is always a global symbol.

Symbol.KeyFor()

This method returns the key from the global symbol registry for the specified global symbol; this is useful if you want to know the key given to any specific global symbol.

Below, I’ll show you how to create a symbol and how each symbol method works.

Code Example

Conclusion

Symbols are great for cryptography, and extremely useful if you want to make unique keys for data  or special functions while the program is running.

With that said, I hope this new feature can help improve your code just a little bit. And, if you think this could help someone else, please share it down below.

Leave a Reply

Your email address will not be published.