开发者

in javascript, same code parsing arguments in different functions - copy or not?

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 f开发者_JAVA百科unctions 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 ...
}


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);
}


Define the functions once and reuse them

var update = function (e) { /* etc */ }
Post.update = update;


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜