0

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.

4 Answers 4

1

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 ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

1) if arguments were stuck into an object then, because of the attribute names, there would be no need of function to parse the arguments - which is what you said at the end. 2) to me not overly pleasing, but agree much better than five args. 3) these funcs are also serve as event triggers, so need to be a wrapper for that as well.
You can pass an object created on the fly direcyl inside your function call, without the need of creating it and storing it into a variable, it's just a literal object.
If you don't need to parse them because you already have them in an object, then it's even better, you don't need that function at all :)
understand. it becomes a lot like named arguments in scheme etc. agree to its utility, but am not enamored with the aesthetics. then little gotchas like having only one attribute by name (arg parsing you can have any number) and funcs being used by triggers, leaves me prefering the parsing - although I agree with everything you said.
That's ok, but then I suggest you to start playing around with those objects because they are really useful in JS programming :)
1

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

4 Comments

as I understand you, sort of like Ruby with without Ruby's syntactic help. this would require that arguments be put into object before passing, and use within the function would be one level indirect, eg, obj.el instead of el.
@cc_young you can always make them local if that made any noticable performance difference. And yes hash objects are the best way for users to insert a large amount of parameters into a function. Think $.ajax
is there anything like php's extract that will pull the attributes from an object and put them into the local scope?
@cc_young no. Why do you care, you micro optimising devil
0

Define the functions once and reuse them

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

1 Comment

do not follow what you're saying in relation to having two different functions parsing arguments the same way - each function is defined once
0

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

1 Comment

so Post.parseArgs would be called within Post.update - then we have the next step of getting the vars out of parseArgs and into the function update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.