var contextMenu = {
isVisible : false,
open : function()
{
//some code here
},
close : function()
{
//some code here
}
}
I’m using this pattern heavily in my code. My application can be done using structural programming without need for classes/object instantiation.
First I used to write something like this:
var isContextMenuVisible : false,
function openContextMenu()
{
//some code here
}
function closeContextMenu()
{
//some code here
}
Is this okay? What is this pattern called? What are the disadvantages?
EDIT: This is called as Singleton pattern
Btw, is it okay to use capitalized names for the object? Like ContextMenu instead of contextMenu? Would that be a proper convention?
Singleton is a design pattern that forces the classes that implement it to offer only one point of access to them. Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves.
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript