Autofill Multiple "null" values in function variable list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RMWChaos
    New Member
    • Oct 2007
    • 137

    Autofill Multiple "null" values in function variable list

    I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function variable list is quite long, and each type of element may not use some of the variables. As a placeholder, I use "null" when there is no value, but this can result in lots of "null" values. So I want to create a function to autofill the "null" values. Here's what I came up with:

    [code=javascript]
    // any one of these could be "null" depending on the DOM element being created

    function createDOM(dom, parent, id, alt, src, method, type, value, size, name, text, onlcick, pSelect)

    {

    var d;

    var null(d);

    var newNull = "null";

    for (null(d); d > 0; --d)

    {

    newNull += ",null";

    };

    null(d) = newNull;

    (...)

    }
    [/code]

    So without the null function, an example createDOM might look like this:

    [code=javascript]
    createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", null, null, null, null, null, null, null, null, "loginForm( )", page.length - 2);
    [/code]

    A real pain in the arse, eh? But with the null function, it would look like this instead:

    [code=javascript]
    createDOM("img" , "divlogin", "loginimg", "Login", "../images/defaultLogin.jp g", "null(8)", "loginForm( )", page.length - 2);
    [/code]

    Which is much better, but perhaps still not great. Am I on the right track, or am I way off base here? Is there a better way to do this, either in the null function or in the original createDOM() variable list?

    Thanks.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    its ugly ;) ... i think you didn't ask in case you didn't find that too? :)

    you may use optional params ... the following way:

    [CODE=javascript]/**
    * @params params object with properties
    */
    function test(params) {
    // now you may do (set a default value in case you don't pass it):
    if (typeof params.prop1 == 'undefined') {
    params.prop1 = 'dflt_value';
    }

    // later on you use params.prop1
    }

    // usage
    test({ prop2: 1, prop3: 2 });
    [/CODE]
    kind regards

    Comment

    • mrhoo
      Contributor
      • Jun 2006
      • 428

      #3
      or, you could always pass the constructor a tag name and an attributes object,
      and never miss your null.

      [CODE=javascript]document.create Dom= function(tag, attr){
      var el= document.create Element(tag);
      for(var p in attr) el[p]= attr[p];
      return el;
      }[/CODE]

      var el=document.cre ateDom('img',{s rc:'imgsrc.gif' ,width:'100px', alt:'sometext'} );

      Comment

      • RMWChaos
        New Member
        • Oct 2007
        • 137

        #4
        Originally posted by gits
        hi ...

        its ugly ;) ... i think you didn't ask in case you didn't find that too? :)

        you may use optional params ... the following way:
        Oh yeah, totally ugly! Which is why I have chosen to use optional params in a JSON property list. By the way, I did find a way to do the null counter with this code:

        [code=javascript]
        function nullcount(d) // Auto-fill null values

        {

        var newNull = null;

        while (--d > 0)

        {

        newNull += " ,null";

        };

        return newNull;

        };
        [/code]

        But I hadn't quite figured out how to remove the quotes around the data that returns, "null, null, null..." instead of null, null, null...so my functions saw it as a single parameter instead of individual ones.

        Anyway, I like the JSON way better because I don't have to worry about the order, null placeholders, or even including all the variables. If I can just get around the excessive recursion error, my code will be golden.

        I'll probably end up posting a new discussion with my new DOMLoader code tonight just to get some other eyes on it looking for ways to improve things, especially if I can't figure out this recursion issue.

        Thanks for your help!

        Comment

        • RMWChaos
          New Member
          • Oct 2007
          • 137

          #5
          Originally posted by mrhoo
          or, you could always pass the constructor a tag name and an attributes object, and never miss your null.
          Hm, maybe it's because I've been looking at JSON property lists for the last 8 hours, but I am having trouble following your code. I need to break it out and play with it a while to understand what you are saying.

          But thank you very much for your reply! You have given me something to learn, which is the best gift anyone can give me! :-)))

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            Originally posted by mrhoo
            or, you could always pass the constructor a tag name and an attributes object,
            and never miss your null.

            [CODE=javascript]document.create Dom= function(tag, attr){
            var el= document.create Element(tag);
            for(var p in attr) el[p]= attr[p];
            return el;
            }[/CODE]

            var el=document.cre ateDom('img',{s rc:'imgsrc.gif' ,width:'100px', alt:'sometext'} );
            yep ;) ... that's it ... slick, short ... the way you should do it ... and combine it with the undefined check i showed you in case you need other attributes set to any default ...

            kind regards

            Comment

            Working...