Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
WoW
Talk
English
Views
Read
Edit
History
More
Search
Navigation
Home
Random page
Help using wiki
Editions
for WoW
for WildStar
for Solar2D
Documentation
for WoW
for WildStar
Reference
WoW
⦁ FrameXML
⦁ AddOns
⦁ API
⦁ WoW Lua
WildStar
⦁ AddOns
⦁ API
⦁ WildStar Lua
Engine
Tools
What links here
Related changes
Special pages
Page information
Site
Recent Changes
Editing
WoW:Object-oriented programming
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Namespace Use === Many Lua programmers consider the use of Lua namespaces as an indicator of a good understanding of the language. This is because it is very organized and requires a good understanding of several language features. Encapsulating your addon within one or a few namespaces can result in allowing your addon to use a minimal number of global variables, thus minimizing its global namespace pollution. Many addons authors create a single namespace which encapsulates all its code. This limits the addon's exposure without limiting functionality. Instead of creating 10 global functions, for example, the addon instead has a single namespace and 10 functions which are accessible within that namespace. The following code segment provides a bad example which unnecessarily pollutes the global namespace, and then provides a functionally equivalent example, which is far more organized. It creates two functions and two variables in the namespace: -- Pollutes the global namespace, making 2 -- global variables and 2 global functions MyAddon_A = 1; MyAddon_B = 2; function MyAddon_PrintA() DEFAULT_CHAT_FRAME:AddMessage(MyAddon_A); end function MyAddon_PrintB() DEFAULT_CHAT_FRAME:AddMessage(MyAddon_B); end -- Creates a "MyAddon" namespace and then creates -- 2 variables, 2 functions inside it MyAddon = {}; MyAddon.a = 1; MyAddon.b = 2; function MyAddon.Printa() DEFAULT_CHAT_FRAME:AddMessage(MyAddon.a); end function MyAddon.Printb() DEFAULT_CHAT_FRAME:AddMessage(MyAddon.b); end As is directly evident, the appropriate use of namespaces can cause code to be far more organized and limit conflicts. This is necessary in an environment such as WoW, where several authors will write several addons, each of whom has no idea what the other is writing, and conflicts must be avoided as much as possible.
Summary:
Please note that all contributions to AddOn Studio are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
AddOn Studio Wiki:Copyrights
for details).
Submissions must be written by you, or copied from a public domain or similar free resource (see
AddOn Studio Wiki:Copyrights
for details).
Cancel
Editing help
(opens in new window)