javascript - Accessing events defined on constructor - is this approach good? -
i have class manages tabs:
function tabsmanager() {}
when performing operations adding or removing tabs broadcasts events, example, active-tab-change
. want make explicit possible events supported class, define them this:
tabsmanager.events = { active_tab_change: "active-tab-change" }
when subscribing tabs manager events, can specify event name this:
tabsmanager.on("active-tab-change", function(){});
however, approach error prone since can misspell event name. use enumeration-like object events
:
tabsmanager.on(tabsmanager.events.active_tab_change, function(){});
the problem here if use dependency injection
don't have direct reference object constructor, i've used approach:
tabsmanager.on(tabsmanager.constructor.events.active_tab_change, function(){});
i've never seen approach used before, i'm curios whether it's feasible approach , whether it's been used in js frameworks/libraries?
Comments
Post a Comment