Scenario
I have an object with a lot of properties and methods, and suppose it's stored in Global.Framework
. Now, I have another object called User
and I want to provide it access to Global.Framework
by cloning the .Framework
to User
.
However, User
also has a property called Name
, (stored in User.Name
) which needs to be passed to each framework's methods as the first argument, transparently.
Example Code
For example, in the method declarations for Global.Framework
, there may be something like
Global.Framework = {
methodOne: function(name, a, b, c) { /* do something */ },
methodTwo: function(name, a) { /* do something */ },
propertyOne: 100,
propertyTwo: "me"
}
However, I want these methods to be exposed to User.Framework
like this: (The properties are simply cloned. They do not need any extra processing)
User.Framework = {
methodOne: function(a, b, c) {
return Global.Framework.methodOne(User.Name, a, b, c);
} (...)
The Problem
Obviously, as the amount of methods in Framework
will change, and perhaps even their arguments, I cannot declare them one by one manually in the cloning process.
What I've tried so far
I've looked up how to get the arguments dynamically and found this: How to get function parameter names/values dynamically from javascript
But I am not sure how to make this happen, and it should preferably not use too many processing resources. This is what I am thinking of:
- Go through every property in the
Framework
object, cloning it if not a function, or - Get the arguments list for the function
- ?? Rewrite the calls to
return functionBeingLooped(User.Name, [the rest of the arguments])
I'm stuck on step 3 and my limited Javascript knowledge reminds me of nothing except eval
(which is out of question). Is there a way to accomplish this?