SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)

    parse arguments as object or function method

    I'm putting together a prototype extension that parses the arguments list of ta function and passes the information to another function.

    At first I was going to extend Object, so that when you call it, the code would read

    Code javascript:
    arguments.parse(function (name, value) {
        ...
    });

    but that would mean the parse function would be an extension of all objects, which while it may be useful for other purposes isn't as far as I want to extend things.

    The other way that I was considering is to extend Function instead, which is seems like a better place to put it but means slightly more complex code to call it.

    Code javascript:
    Function.parse(arguments, function (name, value) {
        ...
    });

    So my question is, where do you consider is the better place to put a parse method.
    1. Object.prototype.parse = function (fn) {};
    2. Function.prototype.parse = function (args, fn) {};
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you provide a use case for parse()? It's unclear from your post what it's supposed to do.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    As an example, you might have a function that accepts several optional parameters in object notation.

    Here is how that function might be called.

    Code javascript:
    doSomething('element', {'width': 200, 'height': 100, 'foo': 'bar', 'speed': 10, 'baz': 'bat'});

    Now normally doSomething() would require some code to parse those properties and then act on them. Something like this:

    Code javascript:
    function doSomething(el) {
        var i,
            arglen = arguments.length,
            arg,
            name;
        for (i = 1; i < argLen; i += 1) {
            arg = arguments[i];
            for (name in arg) if (arg.hasOwnProperty(name)) {
                if (name == 'width' ||
                    name === 'height' ||
                    name === 'speed') {
                    el[name] = arg[name];
                }
                if (name === 'foo' || name === 'baz') {
                    doStuffWith(name, arg[name]);
                }
            }
        }
    }

    So I was thinking that it would be useful if things could be extended so that it's easier to process the above. here is how I've currently extended things:

    Code javascript:
    Function.prototype.parse = function (args, fn) {
        var i,
            argsLen = args.length,
            arg,
            name;
        for (i = 0; i < argsLen; i += 1) {
            arg = args[i];
            for (name in arg) if (arg.hasOwnProperty(name)) {
                fn(name, arg[name], i);
            }
        }
    };

    So that now, the doSomething() function only needs the logic of what's being tested for.

    Code javascript:
    function doSomething(el) {
        Function.parse(arguments, function () {
            if (name == 'width' ||
                name === 'height' ||
                name === 'speed') {
                el[name] = arg[name];
            }
            if (name === 'foo' || name === 'baz') {
                doStuffWith(name, arg[name]);
            }
        }
    }

    So I guess what I'm asking is, is that a good way to go about it, and are there better ways to do so.

    I was at first going to extend Object.prototype instead so that you could instead use arguments.parse(fn) but that didn't seem as good an idea because it's providing that parse method to all objects.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, still not getting it. If the function is called like this

    Code:
    doSomething('element', {'width': 200, 'height': 100, 'foo': 'bar', 'speed': 10, 'baz': 'bat'});
    there is no need to parse anything:

    Code:
    function doSomething(el, params) {
       if(params.width == blah)...
       if(params.foo == "bar") ...
       etc
    }

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Another positive benefit is that it will handle an unknown number of parameters.

    Code javascript:
    doSomething('element', {'width': 200, 'height': 100}, {'speed': 10}, {'foo': 'bar', 'baz': 'bat'});
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But there are only 2 parameters, a string and an object.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I suspect that I may be over-engineering things to cater for strange situations that may never occur.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •