<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Dynamic Drive Forums - Blogs - Twey</title>
		<link>http://www.dynamicdrive.com/forums/blog.php?3374-Twey</link>
		<description>Dynamic Drive help forum</description>
		<language>en</language>
		<lastBuildDate>Sun, 26 May 2013 09:02:07 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>10</ttl>
		<image>
			<url>http://www.dynamicdrive.com/forums/images/misc/rss.jpg</url>
			<title>Dynamic Drive Forums - Blogs - Twey</title>
			<link>http://www.dynamicdrive.com/forums/blog.php?3374-Twey</link>
		</image>
		<item>
			<title>DOM manipulation code</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?8-DOM-manipulation-code</link>
			<pubDate>Tue, 03 Mar 2009 01:13:28 GMT</pubDate>
			<description><![CDATA[Here's some of my personal DOM manipulation code: 
 
 
Code: 
--------- 
Object.extend = function(o1, o2) { 
  for (var x in o2) 
    if (o2.hasOwnProperty(x)) 
      o1[x] = o2[x];]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Here's some of my personal DOM manipulation code:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:372px;">Object.extend = function(o1, o2) {
  for (var x in o2)
    if (o2.hasOwnProperty(x))
      o1[x] = o2[x];

  return o1;
};

Object.extend(Object, {
  copy: function(o) {
    return Object.extend({}, o);
  },

  fromArray: function(o) {
    var r = {};

    Array.map(function(pair) { r[pair[0]] = pair[1]; }, a);

    return r;
  }
});

Object.extend(Function, {
  id: function(a) { return a; }
});

Object.extend(Array, {
  map: function(f, a) {
    for (var i = a.length - 1, r = []; i &gt;= 0; --i)
      r[i] = f(a[i], i);

    return r;
  }
});

Object.extend(String,
  upperFirst: function(s) {
    return s.charAt(0).toUpperCase() + s.substr(1);
  },

  trim: function(s) {
    return s.replace(/^\s+/, &quot;&quot;).replace(/\s+$/, &quot;&quot;);
  }
});

var Css = (function() {
  var jsEquivs = {
    'float' : &quot;css-float&quot;
    // &amp;c.
  };

  function cssToObject(s) {
    return Object.fromArray(Array.map(textToPair, s.split(&quot;;&quot;)));
  }

  function textToPair(s) {
    var r = Array.map(String.trim, s.split(&quot;:&quot;));

    r[0] = propertyToCss(r[0]);

    return r;
  }

  function propertyToJs(s) {
    return Array.map(String.upperFirst, (jsEquivs[s] || s).split(&quot;-&quot;)).join(&quot;&quot;);
  }

  function apply(el, style) {
    if (typeof x === &quot;string&quot;)
      el.setAttribute(&quot;style&quot;, el.style.cssText = style);
    else
      Object.extend(el.style, style);

    return el;
  }

  return {
    propertyToJs: propertyToJs,
    cssToObject: cssToObject,
    apply: apply
  };
})();

var Dom = (function() {
  function get(a) {
    return document.getElementById(a);
  }

  var attrmap = {
    'class': &quot;className&quot;
    // &amp;c.
  };

  function applyAttributes(el, attrs) {
    for (var x in attrs)
      if (attrs.hasOwnProperty(x))
        if (x === &quot;style&quot;)
          Css.apply(el, attrs[x]);
        else
          el[attrmap[x] || x] = attrs[x];

    return el;
  }

  function addChildren(el, children, filter) {
    Array.map(function(child) {
      if (!child) return;

      if (child = create(child))
        el.appendChild(child);
    }, children);
  }

  function create(el, filter) {
    if (typeof el === &quot;string&quot;)
      return document.createTextNode(el);

    if (!(el instanceof Array))
      return el;

    filter = filter || Function.id;

    var r        = document.createElement(el[0]),
        attrs    = el[1] || {},
        children = el[2] || [];

    applyAttributes(r, attrs);

    addChildren(r, children, filter);

    return filter(r);
  }

  function clear(el) {
    while (el.hasChildNodes())
      el.removeChild(el.firstChild);

    return el;
  }

  return {
    create: create,
    get: get,
    clear: clear
  };
})();</pre>
</div>I'll add bits onto it as I need them.</blockquote>

]]></content:encoded>
			<dc:creator>Twey</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?8-DOM-manipulation-code</guid>
		</item>
		<item>
			<title>Generics in Javascript</title>
			<link>http://www.dynamicdrive.com/forums/entry.php?3-Generics-in-Javascript</link>
			<pubDate>Mon, 23 Feb 2009 23:16:55 GMT</pubDate>
			<description><![CDATA[I just used this code to vent some frustration earlier, hope you all like it.  It's pretty self-explanatory.  It's probably too theoretical to go into the DD script archives, so I'll just leave it lying about here. 
 
 
Code: 
--------- 
var Generic = (function() { 
  var MATCH_FAIL = {}, 
     ...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">I just used this code to vent some frustration earlier, hope you all like it.  It's pretty self-explanatory.  It's probably too theoretical to go into the DD script archives, so I'll just leave it lying about here.<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:372px;">var Generic = (function() {
  var MATCH_FAIL = {},
      MATCH_ANY  = function() { return MATCH_ANY; };

  function create(fallback) {
    var s = function() {
      for (var i = 0, args = Array.prototype.slice.call(arguments), r; i &lt; specs.length; ++i)
        if ((r = specs[i].match(args)) !== MATCH_FAIL)
          return r;

      if (s._fallback)
        return s._fallback.apply(this, args);

      throw new Error(&quot;Generic: No methods matched and no fallback provided.&quot;);
    }, specs = s._specs = [];

    s.specialise = specialise;
    s.addFallback = addFallback;
    if (fallback)
      s.addFallback(fallback);

    return s;
  }

  function specialise(patterns, func) {
    var s = this._specs;

    s[s.length] = new Specialisation(patterns, func, this);

    return this;
  }

  function addFallback(func) {
    this._fallback = func;

    return this;
  }

  /**** Begin Specialisation ****/

  function Specialisation(patterns, func, context) {
    this.patterns = patterns;
    this.func = func;
    this.context = context;
  }

  Specialisation.compatible = function(value, pattern) {
    if (pattern === MATCH_ANY &amp;&amp; value !== undefined)
      return true;
    else if ((typeof pattern === &quot;string&quot; || pattern instanceof String) &amp;&amp; typeof value === pattern)
      return true;
    else if (typeof pattern === &quot;function&quot; &amp;&amp; value instanceof pattern)
      return true;
    else if (pattern instanceof Pattern)
      return pattern.guard(value);
    else if (pattern instanceof Interface)
      return pattern.match(value);

    return false;
  };

  Specialisation.prototype = {
    match: function(args) {
      for (var i = 0, a = this.patterns, n = a.length; i &lt; n; ++i)
        if (!Specialisation.compatible(args[i], a[i]))
          return MATCH_FAIL;

      return this.func.apply(this.context, args);
    }
  };

  /**** Begin Pattern ****/

  function Pattern(guard) {
    this.guard = guard;
  }

  function GUARD(func) {
    return new Pattern(func);
  }

  function GUARD_IS(right) {
    return new Pattern(function(val) {
      return val === right;
    });
  }

  /**** Begin Interface ****/

  function Interface(obj) {
    if (!(this instanceof Interface))
      return new Interface(obj);

    for (var x in obj)
      if (obj.hasOwnProperty(x))
        this[x] = obj[x];
  }

  Interface.getSkeleton = function(obj) {
    var r = {};

    for (var x in obj)
      if (obj.hasOwnProperty(x))
        r[x] = typeof obj[x];

    return new Interface(r);
  };

  Interface.prototype = {
    match: function(value) {
      for (var x in this)
        if (this.hasOwnProperty(x) &amp;&amp; !Specialisation.compatible(value[x], this[x]))
          return false;

      return true;
    }
  };

  return {
    create: create,
    Interface: Interface,
    GUARD: GUARD,
    GUARD_IS: GUARD_IS,
    MATCH_ANY: MATCH_ANY
  };
})();</pre>
</div>Some sample code:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:372px;">function Animal(species) {
  this.species = species;
}

var iAnimal = Generic.Interface.getSkeleton(new Animal(&quot;foo&quot;));

var iDog = Generic.Interface({'species' : Generic.GUARD_IS(&quot;dog&quot;)});

var sound = Generic.create()
  .specialise([iDog, 'undefined'], function() {
    print(&quot;I got a dog!  I got one!&quot;);
  })
  .specialise(['string', iAnimal], function(snd, an) {
    print(&quot;A &quot; + an.species + &quot; says '&quot; + snd + &quot;',&quot;);
  })
  .specialise([iAnimal, 'string'], function(an, snd) {
    print(&quot;but a &quot; + an.species + &quot; says '&quot; + snd + &quot;',&quot;);
  })
  .specialise([Animal], function(an) {
    print(&quot;and a &quot; + an.species + &quot; is apparently silent.&quot;);
  })
  .specialise([Generic.MATCH_ANY, 'undefined'], function(val) {
    print(&quot;... and whatever noise a &quot; + val + &quot; makes...&quot;);
  })
  .addFallback(function() {
    print(&quot;Er... random animal noise?&quot;);
  });

sound(new Animal(&quot;dog&quot;));
sound(&quot;woof&quot;, new Animal(&quot;dog&quot;));
sound(new Animal(&quot;cat&quot;), &quot;miaow&quot;);
sound(new Animal(&quot;frog&quot;));
sound(42);
sound(4, 5);</pre>
</div></blockquote>

]]></content:encoded>
			<dc:creator>Twey</dc:creator>
			<guid isPermaLink="true">http://www.dynamicdrive.com/forums/entry.php?3-Generics-in-Javascript</guid>
		</item>
	</channel>
</rss>
