<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Posts about prototype</title>
    <link>http://tobyho.com/tag/prototype</link>
    <language>en-us</language>
    <item>
      <title>Shape-Shifting in Javascript</title>
      <description>&lt;a href="http://www.somethingnimble.com/bliki/mixology"&gt;Mixology&lt;/a&gt;&amp;nbsp;is a Ruby library for what I call &lt;i&gt;shape-shifting&lt;/i&gt;&amp;nbsp;objects.&amp;nbsp;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;shape&lt;/b&gt;&lt;span class="Apple-style-span" style="font-size: 11px; line-height: normal; word-spacing: 0px; font-family: Menlo, monospace; white-space: pre-wrap; -webkit-text-size-adjust: none; "&gt;&lt;b&gt;&#183;&lt;/b&gt;&lt;span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', Verdana, Helvetica, Arial; font-size: 18px; white-space: normal; -webkit-text-size-adjust: auto; line-height: 26px; word-spacing: 2px; "&gt;&lt;b&gt;shifting&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;n. &lt;/i&gt;the act of changing the shape of an object's inheritance hierarchy during runtime.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;It turns out that shape-shifting is incredibly easy to do in Javascript.&lt;pre&gt;ClosedDoor = {
  isClosed: function(){
    return true
  },
  isOpen: function(){
    return false
  }
}

OpenDoor = {
  isClosed: function(){
    return false
  },
  isOpen: function(){
    return true
  }
}

function Door(){
  this.__proto__ = ClosedDoor
  this.open = function(){
    this.__proto__ = OpenDoor
  }
  this.close = function(){
    this.__proto__ = ClosedDoor
  }
}&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Let's give it a go:&lt;/div&gt;&lt;pre&gt;&amp;gt; door = new Door()
Object
&amp;gt; door.isClosed()
true
&amp;gt; door.isOpen()
false
&amp;gt; door.open()
&amp;gt; door.isOpen()
true
&amp;gt; door.isClosed()
false&lt;br&gt;&lt;/pre&gt;&lt;div&gt;In Javascript, "inheritance" is nothing but a link from the child to the parent: the &lt;i&gt;__proto__&lt;/i&gt; property. Once you have this link established, the child inherits all of the properties of the parent. You can change the &lt;i&gt;__proto__&lt;/i&gt; property at runtime. What the example did is simply to change the parent of the door instance to a ClosedDoor or an OpenDoor whenever the state of the door changes. This make for a nice way to implement states. +2 points for prototype inheritance!&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Mon, 26 Apr 2010 01:09:36 -0500</pubDate>
      <guid>http://tobyho.com/Shape-Shifting_in_Javascript</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Shape-Shifting_in_Javascript</link>
    </item>
    <item>
      <title>Typed Deserialization with JSON</title>
      <description>JSON is nice, but if you use it extensively, you go against the OO style of object modeling, which says that behaviors should be attached to objects themselves. Even if you are not an OO bigot, the OO way is sometimes just far more convenient and structures your code better, IMHO. Given my past work with&amp;nbsp;&lt;a href="http://jyaml.sourceforge.net/"&gt;JYaml&lt;/a&gt;, it can't be that hard to read in some JSON and directly deserialize it into a typed object can it?&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Say you have a&amp;nbsp;&lt;i&gt;Person&lt;/i&gt;&amp;nbsp;type:&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;pre&gt;function Person(name){&lt;/pre&gt;&lt;pre&gt;    this.name = name&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;pre&gt;Person.prototype.sayHi = function(){&lt;/pre&gt;&lt;pre&gt;    return "Hi! I am " + this.name&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div&gt;You create an instance called &lt;i&gt;tommy&lt;/i&gt;:&lt;/div&gt;&lt;pre&gt;&amp;gt; tommy = new Person('Tommy')&lt;/pre&gt;&lt;pre&gt;&amp;gt; tommy.sayHi()&lt;/pre&gt;&lt;pre&gt;Hi! I am Tommy&lt;/pre&gt;&lt;div&gt;With a little magic from a &lt;i&gt;toJson&lt;/i&gt;&amp;nbsp;function that I whipped up:&lt;/div&gt;&lt;pre&gt;&amp;gt; tommyJson = toJson(tommy)&lt;/pre&gt;&lt;pre&gt;&amp;gt; tommyJson&lt;/pre&gt;&lt;pre&gt;{__proto__: Person.prototype, name: "Tommy"}&lt;/pre&gt;&lt;div&gt;&lt;i&gt;tommyJson&lt;/i&gt;&amp;nbsp;is now a string representation of &lt;i&gt;tommy&lt;/i&gt;. You can see that it sets the special &lt;i&gt;__proto__&lt;/i&gt;&amp;nbsp;link to &lt;i&gt;Person.prototype&lt;/i&gt;, which will do its magic when it's deserialized.&amp;nbsp;Now let's deserialize it back:&lt;/div&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', Verdana, Helvetica, Arial; font-size: 18px; white-space: normal; "&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;&amp;gt; tommyCopy = fromJson(tommyJson)&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;&amp;gt; tommyCopy.sayHi()&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;Hi! I am Tommy&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;&amp;gt; tommyCopy.constructor&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;function Person(name){&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;    this.name = name;&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;}&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;&amp;gt; tommyCopy === tommy&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;false&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div&gt;As you can see, the deserialized object &lt;i&gt;tommyCopy &lt;/i&gt;is not just a plain object literal, but&amp;nbsp;has all of the behaviors of &lt;i&gt;Person&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "&gt;The code for &lt;i&gt;toJson&lt;/i&gt;&amp;nbsp;and &lt;i&gt;fromJson&lt;/i&gt; is so small I don't even bother putting it on github, so I'll just post it here(You may need&amp;nbsp;&lt;a href="http://github.com/airportyh/ecma5array"&gt;ecma5array&lt;/a&gt;&amp;nbsp;if you want to run it on a sucky browser):&lt;/div&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; font-family: monospace; font-size: 11px; background-color: rgb(238, 238, 238); "&gt;function toJson(obj){
    function keys(o){
        var ret = []
        for (var key in o){
            var val = o[key]
            if (o.hasOwnProperty(key)) // exclude attributes in the parent
                ret.push(key)
        }
        return ret
    }
    function quote(str){
        return '"' + str + '"'
    }
    if (obj === null || obj === undefined)
        return String(obj)
    else if (obj.constructor === String)
        return quote(obj)
    else if (obj.constructor === Array)
        return '[' + obj.map(toJson).join(', ') + ']'
    else if (obj.constructor === Object)
        return '{' + keys(obj).map(function(key){return key + ': ' + toJson(obj[key])}).join(', ') + '}'
    else if (obj.constructor &amp;amp;&amp;amp; obj.constructor.name)
        return '{' + ['__proto__: ' + obj.constructor.name + '.prototype'].concat(
            keys(obj).map(function(key){return key + ': ' + toJson(obj[key])})).join(', ') + '}'
    else
        return String(obj)
}
function fromJson(str){
    return eval('(' + str + ')')
}&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 02 Oct 2009 12:57:33 -0500</pubDate>
      <guid>http://tobyho.com/Typed_Deserialization_with_JSON</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Typed_Deserialization_with_JSON</link>
    </item>
    <item>
      <title>Prototype Inheritence in Python</title>
      <description>&lt;span class="Apple-style-span" style="font-style: italic;"&gt;One of Toby's favorite pastimes is to take concepts from one language and realize it in another.&lt;/span&gt; &lt;span class="Apple-style-span" style="font-style: italic;"&gt;In this episode: Toby implements Javascript-style prototype inheritence in Python.&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;One thing that is super cool about Javascript is the fact that objects are just maps. Not only can you add attributes dynamically at any point, you can also add methods dynamically. In Python, you can add methods dynamically, but normally you'd have to add it into the class. But there are cases when you don't want to bother making a class. This inspired me to see how far I can go in making Python behave more like Javascript.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Introducing prototype.py&lt;br&gt;&lt;/h2&gt;&lt;div&gt;prototype.py lets you write code in Python with Javascript-style prototype inheritence semantics. If you are not familiar with prototype inheritence, just read on for now. I will link to some resources at the end. Now, let's see how to use this bad boy. First you import the module:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; from prototype import *&lt;/pre&gt;&lt;div&gt;To create a &lt;span class="Apple-style-span" style="font-style: italic;"&gt;constructor(we don't really have classes anymore)&lt;/span&gt;, you write a function with the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;@constructor&lt;/span&gt;&amp;nbsp;decorator:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; @constructor
... def Person(this, first, last):
...   this.firstName = first
...   this.lastName = last
...
&amp;gt;&amp;gt;&amp;gt; Person
&amp;lt;constructor 'Person'&amp;gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;I am going to use &lt;span class="Apple-style-span" style="font-style: italic;"&gt;this&lt;/span&gt;&amp;nbsp;rather than &lt;span class="Apple-style-span" style="font-style: italic;"&gt;self&lt;/span&gt;&amp;nbsp;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;camelCase&lt;/span&gt; rather than &lt;span class="Apple-style-span" style="font-style: italic;"&gt;under_scores&lt;/span&gt; for prototype-style code, because, well...Dorothy, we are not in Kansas anymore.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Now you can do:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; bird = Person('Charlie', 'Parker')
&amp;gt;&amp;gt;&amp;gt; bird.firstName
'Charlie'
&amp;gt;&amp;gt;&amp;gt; bird.lastName
'Parker'&lt;br&gt;&lt;/pre&gt;&lt;div&gt;You can add attributes to the object just like in normal Python. But unlike in normal Python, which would raise an&amp;nbsp;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;AttributeError&lt;/span&gt;&amp;nbsp;when trying to access non-existent attributes, in prototype-land it merely returns None:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; print bird.age&lt;/pre&gt;&lt;pre&gt;None&lt;/pre&gt;&lt;div&gt;You can dynamically add a method to the instance just by tagging on a function:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; def sing(this):
...   print '%s sings!!' % this.lastName
...
&amp;gt;&amp;gt;&amp;gt; bird.sing = sing
&amp;gt;&amp;gt;&amp;gt; bird.sing()
Parker sings!!&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This affects only the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;bird&lt;/span&gt;&amp;nbsp;instance. If you want the method to be added to all instances of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Person&lt;/span&gt;&amp;nbsp;however, you can add it to the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;prototype&lt;/span&gt;&amp;nbsp;of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Person&lt;/span&gt;.&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; Person.prototype.sing = sing&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monk = Person('Thelonious', 'Monk')&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monk.sing()&lt;/pre&gt;&lt;pre&gt;Monk sings!!&lt;/pre&gt;&lt;div&gt;This works because &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Person.prototype&lt;/span&gt;&amp;nbsp;is the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;prototype&lt;/span&gt;&amp;nbsp;of the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;monk&lt;/span&gt;&amp;nbsp;instance. In code, this means:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monk.__proto__ == Person.prototype&lt;/pre&gt;&lt;pre&gt;True&lt;/pre&gt;&lt;div&gt;When the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;sing&lt;/span&gt;&amp;nbsp;attribute is not found on the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;monk&lt;/span&gt;&amp;nbsp;instance itself, it will follow the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;__proto__&lt;/span&gt;&amp;nbsp;reference and look it up in its&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&amp;nbsp;&lt;/span&gt;prototype, which is also referred to as its&amp;nbsp;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;parent&lt;/span&gt;. We can manipulate the parent link:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monkJr = Person('T.S.', 'Monk')&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monkJr.__proto__ = monk&lt;/pre&gt;&lt;div&gt;so that now &lt;span class="Apple-style-span" style="font-style: italic;"&gt;monkJr&lt;/span&gt;&amp;nbsp;inherits all of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;monk's &lt;/span&gt;attributes:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monk.hair = 'black and curly'&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; monkJr.hair&lt;/pre&gt;&lt;pre&gt;'black and curly'&lt;/pre&gt;&lt;div&gt;The following are some other properties demonstrating the prototype inheritence model:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; assert monkJr.constructor == monk.constructor == Person&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; assert Object.prototype.constructor == Object&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; assert Person.prototype.constructor == Person&lt;/pre&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; assert Person.prototype.__proto__ == Object.prototype&lt;/pre&gt;&lt;div&gt;If this seems confusing, remember that in prototype-land, there are two kinds of things: &lt;span class="Apple-style-span" style="font-style: italic;"&gt;constructors&lt;/span&gt;&amp;nbsp;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;instances&lt;/span&gt;. The prototype of a constructor is nothing more than an instance of the type that the constructor represents. A new instance returned from calling a constructor will have its parent set to the prototype of the constructor.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Things You Can't Do in Javascript, i.e. the 1+1&amp;gt;2 Effect&lt;br&gt;&lt;/h2&gt;&lt;div&gt;Python is a more powerful and flexible language than Javascript, so, why am I dumbbing it down to Javascript's level? To show that I am &lt;span class="Apple-style-span" style="font-style: italic;"&gt;not&lt;/span&gt;&amp;nbsp;trying to do that at all, let me point out that there are at least a couple of really cool things you can do with prototype.py that you can't with Javascript, simply due to the fact that it is in Python.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;The first thing is properties. With prototype.py, you can add properties to objects like so:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; def getName(this):
...   return '%s %s' % (this.firstName, this.lastName)
...
&amp;gt;&amp;gt;&amp;gt; Person.prototype.name = property(getName)
&amp;gt;&amp;gt;&amp;gt; bird.name
'Charlie Parker'&lt;br&gt;&lt;/pre&gt;&lt;div&gt;You can also specify setters and deleters in the way you'd expect:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; def setName(this, name):
...   first, last = name.split(' ')
...   this.firstName = first
...   this.lastName = last
...
&amp;gt;&amp;gt;&amp;gt; def deleteName(this):
...   del this.firstName
...   del this.lastName
...
&amp;gt;&amp;gt;&amp;gt; Person.prototype.name = property(getName, setName, deleteName)
&amp;gt;&amp;gt;&amp;gt; bird.name = 'Dizzy Gillespie'
&amp;gt;&amp;gt;&amp;gt; bird.name
'Dizzy Gillespie'
&amp;gt;&amp;gt;&amp;gt; del bird.name
&amp;gt;&amp;gt;&amp;gt; bird.name
'None None'&lt;br&gt;&lt;/pre&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;The second thing is&amp;nbsp;&lt;a href="/Parameter_List_Chaining_in_Python"&gt;named parameters, keyword parameters and optional parameters&lt;/a&gt;. Let's just do an example with keyword parameters. Let's say I just wanted a way to easily create ad-hoc objects as key-value pairs, I could write:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; @constructor&lt;/pre&gt;&lt;pre&gt;... def Data(this, **kwargs):
...   for key in kwargs.keys():
...     setattr(this, key, kwargs[key])
...&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Which lets me write:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; project = Data(project='prototype.py', language='python')&lt;br&gt;&lt;/pre&gt;&lt;div&gt;But, I could also define methods and properties in this way:&lt;/div&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; file = Data(fileName='prototype.py', fileExt=property(&lt;span class="Apple-style-span" style="font-style: italic;"&gt;getExt)&lt;/span&gt;,&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;                    read=&lt;span class="Apple-style-span" style="font-style: italic;"&gt;read&lt;/span&gt;, write=&lt;span class="Apple-style-span" style="font-style: italic;"&gt;write&lt;/span&gt;)&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Sweet!&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;A Note About the Implementation&lt;br&gt;&lt;/h2&gt;&lt;div&gt;The implementation of prototype.py is only about 60 lines of Python at time of writing, would have gone down to 40 without property support. The code can be found&amp;nbsp;&lt;a href="http://github.com/airportyh/misc/tree/86d360ef5b449f4d2ceb795fbf6015341a82d491/prototype.py"&gt;here.&lt;/a&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;A big discovery that enabled me to do this was the&amp;nbsp;&lt;a href="/http://docs.python.org/library/new.html"&gt;new&lt;/a&gt;&amp;nbsp;module. It allowed me to take a function and make it into an instance method by binding it to an object:&lt;/div&gt;&lt;pre&gt;method = new.instancemethod(&lt;span class="Apple-style-span" style="font-style: italic;"&gt;function&lt;/span&gt;, &lt;span class="Apple-style-span" style="font-style: italic;"&gt;object&lt;/span&gt;)&lt;br&gt;&lt;/pre&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Another note is that I made the design decision that for methods, I chose to store the unbound function rather than the bound method in the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;__dict__&lt;/span&gt;&amp;nbsp;of the object. I would then bind the function on the fly when it's asked for. This made it very easily to inherit methods because you don't have to worry that the method is really bound to the parent rather than the instance in question.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Further Reading About Prototype Inheritence&lt;br&gt;&lt;/h2&gt;&lt;div&gt;If you want to learn more about prototype inheritence, try:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://mckoss.com/jscript/object.htm"&gt;Mike Koss' article&lt;/a&gt;&lt;br&gt;&lt;/li&gt;&lt;li&gt;the &lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming"&gt;Wikipedia article&lt;/a&gt;&lt;/li&gt;&lt;li&gt;my short post trying to&amp;nbsp;&lt;a href="/Dissecting_the_Prototype_Chaining"&gt;Dissect the Prototype Chaining&lt;/a&gt;&lt;/li&gt;&lt;li&gt;or if you have a lot of time,&amp;nbsp;&lt;a href="http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html"&gt;this lengthy essay from Stevey.&lt;/a&gt;&amp;nbsp;&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 23 May 2009 13:04:39 -0500</pubDate>
      <guid>http://tobyho.com/Prototype_Inheritence_in_Python</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Prototype_Inheritence_in_Python</link>
    </item>
    <item>
      <title>Modifying Core Types in ActionScript 3 Using the Prototype Object</title>
      <description>ActionScript 3 has a Javascript lineage. It was essentially a fork of Ecmascript. Plus, the Adobe team has worked hard on making the language a proper superset of the Ecmascript specification. This is why although ActionScript 3 was a major architechtural change from 2, and as a result became much more like Java than Javascript, it still has support for the&amp;nbsp;&lt;a href="Dissecting the Prototype Chaining"&gt;prototype object&lt;/a&gt;.&amp;nbsp;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;When I learned about the prototype object, it was a fresh air from traditional class-based OOP. Studying cool libraries like&amp;nbsp;&lt;a href="http://prototypejs.org"&gt;prototype.js&lt;/a&gt;&amp;nbsp;made me realize that the prototype model makes javascript far more flexible than some other strictly class-based OO languages.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;One of the cool things you can do with the prototype object in javascript is modify the core classes of the language, like &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Array, String, &lt;/span&gt;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Date&lt;/span&gt;. You can do this in ActionScript 3 too, for it conforms to Ecmascript 4. For example, let's say you want to write the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;function for arrays &lt;span class="Apple-style-span" style="font-style: italic;"&gt;a la&lt;/span&gt; ruby. You would do:&lt;/div&gt;&lt;pre&gt;Array.prototype.collect = function(f){&lt;/pre&gt;&lt;pre&gt;  var ret = [];&lt;/pre&gt;&lt;pre&gt;  for each(var it in this) ret.push(f(it));&lt;/pre&gt;&lt;pre&gt;  return ret;&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div&gt;There you see me using the nice &amp;nbsp;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;for each&lt;/span&gt;&amp;nbsp;syntax. But, this is going to break the behavior of the array, because now the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;function is going to show up in the enumeration in the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;for each&lt;/span&gt;&amp;nbsp;loops. We don't want that, so to fix that we are going to set the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;attribute of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Array.prototype&lt;/span&gt; to not be enumerable:&lt;/div&gt;&lt;pre&gt;Array.prototype.setPropertyIsEnumerable('collect', false);&lt;/pre&gt;&lt;div&gt;This allows you to write the following code:&lt;/div&gt;&lt;pre&gt;[1,2,3].collect(function(i){ return i * 2; });&lt;/pre&gt;&lt;pre&gt;// result would be [2,4,6]&lt;/pre&gt;&lt;div&gt;It's kinda tedious to have to &lt;span class="Apple-style-span" style="font-style: italic;"&gt;setPropertyIsEnumerable&amp;nbsp;&lt;/span&gt;for every time we add a function to an existing type, so I wrote a convience function:&lt;br&gt;&lt;/div&gt;&lt;pre&gt;function addMethodsTo(cls:Class, methods){
    for (var name:String in methods){
        cls.prototype[name] = methods[name];
        cls.prototype.setPropertyIsEnumerable(name, false);
    }
}&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Which you can use like so:&lt;/div&gt;&lt;pre&gt;addMethodsTo(Array, {
    collect: function(f){
        var ret = [];
        for each(var it in this) ret.push(f(it));
        return ret;
    },&lt;/pre&gt;&lt;pre&gt;    anotherMethod: function(){&lt;/pre&gt;&lt;pre&gt;        ...&lt;/pre&gt;&lt;pre&gt;    },&lt;/pre&gt;&lt;pre&gt;    ...
});&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This is sort of like the style prototype.js uses for extending/creating classes.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;A Word of Caution&lt;br&gt;&lt;/h2&gt;&lt;div&gt;Before you consider going further with this, I must advice you to think twice before using this technique(however, I hope you &lt;span class="Apple-style-span" style="font-style: italic;"&gt;do&lt;/span&gt; decide to use it afterwards ;). There are several caveats:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;The prototype-based style is a second-class citizen in the world of ActionScript 3 and Flex. The Adobe team as well as the community seem to be much more committed to the class-based approach. I will describe some of the rough edges below.&lt;/li&gt;&lt;li&gt;You will give up compile time type checking for the portions of your code that use this style.&lt;/li&gt;&lt;li&gt;Prototype inheritence is handled by a completely different mechanism than class-based inheritence in the Flash VM and is not as performant.&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Where to put this Code?&lt;br&gt;&lt;/h2&gt;&lt;div&gt;You saw the code example above, but, where do you put it? Since I decided to use a helper function(&lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodsTo)&lt;/span&gt;, the code cannot be directly pasted inside the class scope of a class unless the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodsTo&lt;/span&gt;&amp;nbsp;function is declared to be static(you can only make a method call directly inside class scope if it is a class method). As, a general solution, I'd rather the code be portable. So it should be includable in both class and function scope, and also, I'd like it not to pollute any namespaces.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;My current solution is to put this bit of code inside an anonymous function which immediately gets executed:&lt;/div&gt;&lt;pre&gt;// contents of includes/Array.as&lt;/pre&gt;&lt;pre&gt;(function(){
    include 'addMethodsTo.as';
    addMethodsTo(Array, {
        collect: function(f){
            var ret = [];
            for each(var it in this) ret.push(f(it));
            return ret;
        }
    });
})();&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The &lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodTo &lt;/span&gt;function&amp;nbsp;is pulled into a separate file to be easily includable else where.&lt;/div&gt;&lt;div&gt;So, with this, I would do the following to include this Array functionality:&lt;/div&gt;&lt;pre&gt;include 'includes/Array.as';&lt;/pre&gt;&lt;div&gt;Head over to&amp;nbsp;&lt;a href="http://github.com/airportyh/misc/tree/aff1496c9f99ff890801e5ecacdefdbf78be1016/prototype_in_as"&gt;Github&lt;/a&gt;&amp;nbsp;for the complete structure of the files.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;These methods are added during runtime - at exactly the time the above line of included code is executed, and not a moment before. I like to do the include at the top level of the application, this way the entire program has immediate access to the new methods. Oh, and when I said &lt;span class="Apple-style-span" style="font-style: italic;"&gt;entire program&lt;/span&gt;, I do mean the entire program - not just the files that happen to include the file. Well, this is good &lt;span class="Apple-style-span" style="font-style: italic;"&gt;and&lt;/span&gt;&amp;nbsp;bad. This means if you create components that use the array extensions you've created without explicitly including it(you've included it at the entry point of your program), then you have created an &lt;span class="Apple-style-span" style="font-style: italic;"&gt;invisible dependency&lt;/span&gt;. If you try to take the component and use it in a different project without the array extensions, it will not work. Of course, you could also make the dependency explicit by including the file everywhere you are using them, but 1) that's kinda tedious/repetitious, and 2) there's nothing enforcing you to do this.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Pitfalls and Gotchas&lt;br&gt;&lt;/h2&gt;&lt;div&gt;As I mentioned, the prototype-based programming style is a second-class citizen in the ActionScript 3 world, and so, its use is not particularly well supported. First of all, the compiler does not recognize any of the methods added via the prototype mechanism, and thus cannot perform static type checking on them. But what is funny is the way the compiler copes with this - it depends...on the type in question. For Arrays, the compiler simply allows all method calls - you can call any method on an array, even if it doesn't exist, the compiler won't complain. So, for a call like:&lt;/div&gt;&lt;pre&gt;[1, 2, 3].collect(...);&lt;/pre&gt;&lt;div&gt;The compiler won't even say a word. But for Dates, it gives a warning message. This:&lt;/div&gt;&lt;pre&gt;new Date().format()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;would trigger this warning:&lt;/div&gt;&lt;pre&gt;Warning: format is not a recognized method of the dynamic class Date.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;But, for strings, it's a different story still:&lt;/div&gt;&lt;pre&gt;'one, two, three'.csv2Array()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;compiler says:&lt;/div&gt;&lt;pre&gt;Error: Call to a possibly undefined method csv2Array through a&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;    reference with static type String.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;And the same thing with numbers:&lt;/div&gt;&lt;pre&gt;(2).minutes().ago()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;compiler says:&lt;/div&gt;&lt;pre&gt;Error: Call to a possibly undefined method minutes through a&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;    reference with static type int.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The work around for strings and numbers is to upcast it to an Object:&lt;/div&gt;&lt;pre&gt;Object('one, two, three').csv2Array();&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;Object(2).minutes().ago();&lt;br&gt;&lt;/pre&gt;&lt;div&gt;or if you just have an untyped variable, it'll work just fine:&lt;/div&gt;&lt;pre&gt;var x = 2;&lt;/pre&gt;&lt;pre&gt;x.minutes().ago();&lt;/pre&gt;&lt;div&gt;See the full code example&amp;nbsp;&lt;a href="http://github.com/airportyh/misc/blob/366479f05b6ef862bd48a83654b511fcd18b3b20/prototype_in_as/ex1.mxml"&gt;here&lt;/a&gt;, and the&amp;nbsp;&lt;a href="http://tobyho.com/uploads/flex_examples/prototype/ex1.swf"&gt;demo&lt;/a&gt;&amp;nbsp;here.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;What about runtime error handling? So let's see what happens if you don't include the extensions and run the code. When I took out the array extension methods, the runtime dutifully throw me the:&lt;/div&gt;&lt;pre&gt;TypeError: Error #1006: collect is not a function.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This is good, just what I would expect. For Date, it works the same way. Now let's try taking out the string extensions:&lt;/div&gt;&lt;pre&gt;TypeError: Error #1006: value is not a function.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Uhh, what? Not really sure what you mean.&lt;/span&gt; And as you would expect, it works like this for Number&amp;nbsp;as well.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I have also seen cases where the runtime simply completely &lt;span class="Apple-style-span" style="font-style: italic;"&gt;muffles&lt;/span&gt; an error when there's an undefined method being tried as someone documented&amp;nbsp;&lt;a href="http://turbidwater.blogspot.com/2007/12/flex-sucks-ass.html"&gt;here&lt;/a&gt;. But I am not able to reproduce this just now. Also on another note, the error stacktrace from the flash player(debug version) is not very helpful because although it gives the call stack, there are no line numbers. I am sure that you'd get a better experience using Flex Builder, however.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I think that about wraps it up. Although extending core types is fun, powerful, and elegent, it's also full of holes and flying scissors everywhere. Are &lt;span class="Apple-style-span" style="font-style: italic;"&gt;you&lt;/span&gt;&amp;nbsp;ready to jump into this brave new world?&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;A message to the Adobe Flex/ActionScript team: I implore you to put more effort into the prototype-based side of the language. It allows for many possibilities which its class-based counterpart cannot offer. I don't dislike the class-based approach. I think the two each have their own strengths and weakness. Which is why I love this hybrid aspect of ActionScript 3 which allows me to use either style in the same environment. I believe that if the prototype-based aspect of ActionScript were to improve and get more exposure, it would not only become a better language, but also a more wide spread language.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 02 May 2009 21:41:27 -0500</pubDate>
      <guid>http://tobyho.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object</link>
    </item>
    <item>
      <title>Dissecting the Prototype Chain</title>
      <description>I realized when I was trying to explain the prototype chain to my wife the other day that I didn't get it completely. So I went back to the drawing board and this is the example that I came up with:&lt;br&gt;&lt;br&gt;&lt;pre&gt;function&#160;inherit(parent){&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;var&#160;obj&#160;=&#160;function(){};&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;obj.prototype&#160;=&#160;parent;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;return&#160;new&#160;obj();&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;}&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;toby&#160;=&#160;{};&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;toby.name&#160;=&#160;'Toby';&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;toby.age&#160;=&#160;27;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;toby.eyeColor&#160;=&#160;'brown';&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;toby.hairColor&#160;=&#160;'dark';&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;console.log(toby);&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;//&#160;Object&#160;name=Toby&#160;age=27&#160;eyeColor=brown&#160;hairColor=dark&lt;/pre&gt;&lt;pre&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;emma&#160;=&#160;inherit(toby);&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;emma.name&#160;=&#160;'Emma';&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;emma.age&#160;=&#160;1&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;console.log(emma);&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;//&#160;Object&#160;name=Emma&#160;age=1&#160;eyeColor=brown&#160;hairColor=dark&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;console.log("Toby&#160;is&#160;Emma's&#160;parent:&#160;"&#160;+&#160;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;	&lt;/span&gt;(emma.__proto__&#160;==&#160;toby));&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;//&#160;true&lt;br&gt;&lt;/pre&gt;</description>
      <pubDate>Mon, 08 Dec 2008 14:36:17 -0600</pubDate>
      <guid>http://tobyho.com/Dissecting_the_Prototype_Chain</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Dissecting_the_Prototype_Chain</link>
    </item>
    <item>
      <title>Making justtodolist work for chrome and safari</title>
      <description>Since &lt;a href="/Google_Chrome"&gt;Chrome &lt;/a&gt;is so great, I wanted to use it for all my browser needs. But the apps I've been working on myself have not been targeted for WebKit - the rendering engine used by chrome, and also used my Safari. This is mostly due to laziness on my part - having to handle two browsers at a time is chaos enough. But Safari compatibility is on my todo list for more than one of my projects, and chrome is a good push for this cause. And so I went in today and made &lt;a href="http://justtodolist.appspot.com"&gt;&lt;i&gt;just todo list. &lt;/i&gt;&lt;/a&gt;compatible with Chrome and Safari. The differences I encountered were:&lt;br&gt;&lt;ol&gt;&lt;li&gt;This is really prototype specific - prototype adds an empty underscore parameter for all Ajax request for WebKit browsers. Because apparently, Safari errors out when you try to post with an empty body on XHR calls. And, who knows, this might already be fixed in WebKit. I happened to have code that was not handling this fact on the server side. This was no biggy.&lt;/li&gt;&lt;li&gt;This is an error on my part really, but I had a form with a text input inside. But also registered for the key events on the text input so that when enter is pressed I called form.submit(). The key event code is really unnecessary because an enter on the text input triggers the form submit on both FF and IE. So, although on the other 2 browsers this was no problem, in WebKit this triggers 2 posts. So... just DON'T do this.&lt;br&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;</description>
      <pubDate>Tue, 02 Sep 2008 23:01:32 -0500</pubDate>
      <guid>http://tobyho.com/Making_justtodolist_work_for_chrome_and_safari</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Making_justtodolist_work_for_chrome_and_safari</link>
    </item>
    <item>
      <title>Play Date with jQuery</title>
      <description>I had a new toy project and started out with my trusty sidekick: prototype.js but a bit of the way in i decide to try my hand at jQuery instead, since I've already read a lot about jQuery and really like the &lt;a href="http://osteele.com/archives/2008/02/inlined-frommaybe" mce_href="http://osteele.com/archives/2008/02/inlined-frommaybe"&gt;monad-like paradigm&lt;/a&gt; which allows you to write elegant "flow" code without having to worry about null values anywhere in the chain.&lt;br&gt;&lt;br&gt;It was quite easy translating my existing prototype.js code to jQuery(probably just around 30 lines). &lt;br&gt;&lt;ul&gt;&lt;li&gt;The end result was probably 20% more succient.&lt;/li&gt;&lt;li&gt;I love the css 3 capable selectors, it's very refreshing to be able to use attribute and partial string match selectors which normally you don't get to use for your work.&amp;nbsp;&lt;/li&gt;&lt;li&gt;I also like the way attributes are set as chained methods(again, in the monad flavor), you do something like &lt;font face="courier new,courier"&gt;$('#myid').attr('value')&lt;/font&gt; to get the value and &lt;font face="courier new,courier"&gt;$('#myid').attr('value', 1)&lt;/font&gt; to set the value to 1. This again, allows you to chain stuff together. What's the point of this? ...I guess I just like the monad thing.&lt;/li&gt;&lt;li&gt;I think the jQuery syntax for behavioral javascript is prettier than prototype.js + lowpro.js&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;I really have nothing bad to say about jQuery. Even the UI components seem pretty on par with prototype.js, there's jqueryui, from which I used the autocomplete plugin. It's a little bit unfortunate that I had already invested about 1800 lines of javascript code on writing javascript components based on prototype.js and scriptaculous at work, otherwise had I started over I would very probably use jQuery. I will definitely go jQuery for my other projects.&lt;br&gt;&lt;br&gt;Are there anything from prototype.js that I miss? Well, yes, I miss the array extensions from prototype, which lets you use ruby/smalltalk/lisp-like internal iterator style list operations. jQuery has a $.each, but it's not quite as pretty... but it gets the job done. And I haven't looked, but I am not sure there's an equivalent to prototype's Event.KEY_RETURN, Event.KEY_BACKSPACE and such in jQuery. Also, I had gotten comfortable to the OO model from prototype, and doing without it will be different, but then again I am adaptable. Plus, there's nothing at all stopping you from using jQuery and prototype.js together.&lt;br&gt;</description>
      <pubDate>Sun, 31 Aug 2008 23:47:39 -0500</pubDate>
      <guid>http://tobyho.com/Play_Date_with_jQuery</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Play_Date_with_jQuery</link>
    </item>
    <item>
      <title>FF3 vs FF2 setting of position and width</title>
      <description>Wow, FF3 broke my app!&lt;br&gt;&lt;br&gt;I had a line like this(sorry this code uses prototype.js, but it's not specific to it):&lt;br&gt;&lt;font face="courier new,courier"&gt;myDiv.setStyle({&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; position: 'absolute',&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; top: elmPos.top + elm.offsetHeight,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; left: elmPos.left,&lt;br&gt;
&amp;nbsp; &amp;nbsp; width: elm.offsetWidth - 5});&lt;/font&gt;&lt;br&gt;&lt;br&gt;This won't work anymore because starting FF3, you can't set the attributes: top, left, width, height to numeric values anymore, they have to labeled with a unit, like so:&lt;br&gt;&lt;font face="courier new,courier"&gt;myDiv.setStyle({&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; position: 'absolute',&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; top: asPx(elmPos.top + elm.offsetHeight),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; left: asPx(elmPos.left),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; width: asPx(elm.offsetWidth - 5)});&lt;/font&gt;&lt;br&gt;&lt;br&gt;Where asPx is:&lt;br&gt;&lt;font face="courier new,courier"&gt;function asPx(n){&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return n + 'px';&lt;br&gt;}&lt;/font&gt;&lt;br&gt;</description>
      <pubDate>Mon, 23 Jun 2008 21:45:42 -0500</pubDate>
      <guid>http://tobyho.com/FF3_vs_FF2_setting_of_position_and_width</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/FF3_vs_FF2_setting_of_position_and_width</link>
    </item>
    <item>
      <title>Use up() instead of parentNode</title>
      <description>With prototype.js, you should use element.up() instead of element.parentNode. This is again because of &lt;a href="/FF%27s_built_in_Element_class"&gt;this browser difference&lt;/a&gt;.&lt;br&gt;</description>
      <pubDate>Wed, 04 Jun 2008 16:12:08 -0500</pubDate>
      <guid>http://tobyho.com/Use_up%28%29_instead_of_parentNode</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Use_up%28%29_instead_of_parentNode</link>
    </item>
    <item>
      <title>Prototype's Element dot extend</title>
      <description>Element.extend is an important method is the prototype framework. It adds all the helper/mixin methods to a dom element, like this:&lt;br&gt;Element.extend(myElem);&lt;br&gt;If for some reason you have to do this manually(probably in IE), this is what you need.&lt;br&gt;</description>
      <pubDate>Wed, 04 Jun 2008 12:26:37 -0500</pubDate>
      <guid>http://tobyho.com/Prototype%27s_Element_dot_extend</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Prototype%27s_Element_dot_extend</link>
    </item>
    <item>
      <title>Constructor Chaining in Prototype</title>
      <description>Ever wonder how to do constructor chaining in prototype.js?&lt;br&gt;Here's how:&lt;br&gt;&lt;br&gt;&lt;pre&gt;var&#160;Person&#160;=&#160;Class.create({&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160;initialize:&#160;function(){&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;console.log('hello&#160;world!');&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160;}&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;});&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;var&#160;Pirate&#160;=&#160;Class.create(Person,{&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160;initialize:&#160;function($super){&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;console.log('before&#160;super');&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;$super();&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160; &#160;console.log('after&#160;super');&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&#160;&#160;}&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;});&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;new&#160;Person();&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;new&#160;Pirate();&lt;br&gt;&lt;/pre&gt;&lt;br&gt;The $super parameter is "special", and is detected by the library. It gives you a reference to the method of the same name defined in the super class(Ruby style, basically). Notice that unlike Java, you can do stuff before super's constructor is called. For more, see &lt;a href="http://www.prototypejs.org/learn/class-inheritance" mce_href="http://www.prototypejs.org/learn/class-inheritance"&gt;here&lt;/a&gt;.&lt;br&gt;</description>
      <pubDate>Tue, 20 May 2008 21:32:26 -0500</pubDate>
      <guid>http://tobyho.com/Constructor_Chaining_in_Prototype</guid>
      <author>toby ho</author>
      <link>http://tobyho.com/Constructor_Chaining_in_Prototype</link>
    </item>
  </channel>
</rss>
