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.
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 ...
}