Given this setup:
var ObjectNamespace = {}
var FunctionNamespace = function FunctionNamespace() {}
ObjectNamespace.User = function() {}
FunctionNamespace.User = function User() {}
Is there anyway to write a namespace
method to accomplish something like this:
ObjectNamespace.User.namespace() //=> "ObjectNamespace.User"
FunctionNamespace.User.namespace() //=> "FunctionNamespace.User"
You can get the first node by doing something like this:
ObjectNamespace.User.namespace = function() {
return this.toString().match(/function *(\w+)/)[1]
}
ObjectNamespace.User.namespace() //=> "User"
But my question is, is there a way to reference the "parent" object, i.e. the object the User
function is defined on (ObjectNamespace
)? Something meta along the lines of arguments.caller.callee
. That way you'd get this:
ObjectNamespace.User.namespace() //=> "ObjectNamespace.User"
Any ideas?