Reputation: 20213
Have a series of functions, each of which parses its arguments identically. right now this parsing chunk is cut and pasted to the beginning of each function. Is there a better way to do this?
Post.update = function( e ) { // parse args var e, el, orig_el, args, render, callBack; for( var i=0; i<arguments.length; i++ ) { if( arguments[i] instanceof HTMLElement ) orig_el = arguments[i]; else if( arguments[i] instanceof Render ) render = arguments[i]; else if( arguments[i] instanceof Event ) { e = arguments[i]; orig_el = orig_el || e.target; } else if( typeof arguments[i] == 'function' ) callBack = arguments[i]; } // Post.update work here } Post.verify = function( e ) { // parse args ... // Post.verify work here }
The reasons arguments are parsed instead of passed individually are that, with five+ possible args
I'm prone to make mistakes in ordering and omission calling funcs with a long list of args
changing one argument to the function means changing every call to the func
imho a function with five arguments is quite unreadable compared to passing exactly what's necessary
So my goal is to abstract the parse section of the functions while maintaining the clarity of the function calls as they are now.
Upvotes: 0
Views: 955
Reputation: 12294
Of course you should use a function to prevent copy and paste all that code!
What if you want to change a single character in the code? Right now you have to copy and paste everything again! That's just no good really.
Stick that code into a function and call it everytime, this way when you change something in the code function, the change will be reflected to every place where that function is being used.
Are you worried about passing so many arguments into the function? You really have to be worried, cause passing so many arguments into a function is really bad. Just stick all your arguments into a single object, and only pass that object to the function, like this:
var arg = {
arg1: 'bla bla bla',
arg2: 4,
//.....
};
// call the function passing only one parameter
myFunction( arg );
Then inside the function you will have a single argument and you can access all the others like this:
function myFunction( arg ) {
// arg.arg1
// arg.arg2 ...
}
Upvotes: 1
Reputation: 169401
Rather then parsing 5 arguments out of a function you should realise functions should not have more then 3 arguments.
What you want to do instead is have the last argument be an object
Post.update = function(ev, obj) {
// obj.el
// obj.render
// obj.ev
// obj.cb
}
Then just have a function for that can be re-used
Post.parseObj = function(obj) {
...
}
Post.update = function(ev, obj) {
obj = Post.parseObj(obj);
}
Upvotes: 1
Reputation: 7315
Why not using a Post.parseArgs function ?
Post.parseArgs = function(args)
{
// parse args
var e, el, orig_el, args, render, callBack;
for( var i=0; i<arguments.length; i++ ) {
if( arguments[i] instanceof HTMLElement ) orig_el = arguments[i];
else if( arguments[i] instanceof Render ) render = arguments[i];
else if( arguments[i] instanceof Event ) {
e = arguments[i];
orig_el = orig_el || e.target;
}
else if( typeof arguments[i] == 'function' ) callBack = arguments[i];
}
// return what you want...
}
Upvotes: 0
Reputation: 943579
Define the functions once and reuse them
var update = function (e) { /* etc */ }
Post.update = update;
Upvotes: 0