Guest
Public paste!

Clint

By: a guest | Mar 24th, 2009 | Syntax: JavaScript | Size: 3.31 KB | Hits: 313 | Expires: Never
Copy text to clipboard
  1. /*
  2.     A snippet with some methods I'm been playing with...
  3.  
  4.     Curious what people think of the method "createElementWithProps"... worth the overhead? Easier way to do it?
  5. */
  6.  
  7.     var FunctionObject = function() {
  8.        
  9.         var _e = [], _d = document;
  10.        
  11.         return {
  12.             /**
  13.              * Creates an element within the document, without a parent, as if by <code>document.createElement</code>. This method
  14.              * has better performance in some browsers, as it caches instances of created objects and clones them, rather than manipulate the
  15.              * document directly.
  16.              * @param {String} t The tag name of the element to create.
  17.              * @return {Node} A new element.
  18.              *
  19.              * @static
  20.              * @function
  21.              * @fullname Create Element
  22.              */
  23.             createElement : function (t) {
  24.                 var a = _e[t];
  25.                 if (!a) {
  26.                     a = _e[t] = _d.createElement(t);
  27.                 }
  28.                 if (!a) {
  29.                     return null;
  30.                 }
  31.                 else {
  32.                     return a.cloneNode(false);
  33.                 }
  34.             },
  35.            
  36.             /**
  37.              * Creates an element within the document, without a parent, as if by <code>document.createElement</code>. Any
  38.              * given properites will then be set onto the newly created element. This method has better performance, as it
  39.              * caches instances of created objects and clones them, rather than manipulate the document directly.
  40.              * @param {String} t The tag name of the element to create.
  41.              * @param {Object} [p] The properties to set onto the created element, (e.g. <code>{ "href" : "index.html", "name" : "theName"}</code>).
  42.              * @return {Node} A new element.
  43.              *
  44.              * @static
  45.              * @function
  46.              * @fullname Create Element with Properties
  47.              */
  48.             createElementWithProps : function (t, p) {
  49.                 var e = this.createElement(t);
  50.                 return this.mergeObjects(e, p);
  51.             },
  52.            
  53.             /**
  54.              * Merges two option objects.
  55.              * @param {Object} o1 The option object to be modified.
  56.              * @param {Object} o2 The option object containing properties to be copied.
  57.              * @param {Boolean} d True if properties on o1 should be immutable, false otherwise.
  58.              * @return {Object} An object containing properties.
  59.              *
  60.              * @static
  61.              * @function
  62.              * @fullname Merge Objects
  63.              */
  64.             mergeObjects : function (o1, o2, d) {
  65.                 o1 = o1 || {};
  66.                 o2 = o2 || {};
  67.                 var p;
  68.                 for (p in o2) {
  69.                     if (p) {
  70.                         o1[p] = (o1[p] === undefined) ? o2[p] : !d ? o2[p] : o1[p];
  71.                     }
  72.                 }
  73.                 return o1;
  74.             }
  75.         };
  76.     }();
  77.  
  78.     var alertAnchor = FunctionObject.createElementWithProps("A", {
  79.         "href" : "#",
  80.         "name" : "alertLink",
  81.         "className" : "alert-me",
  82.         "innerHTML" : "Click Me!",
  83.         "onclick" : function() {
  84.             alert('Hi!');
  85.         }
  86.     });