Useful lies about JavaScript Prototype Model

time to read 2 min | 298 words

Note: This is not how it works, but it is close enough that it is a valid mental model for most scenarios.

When I think about JavaScript and its type system, I usually envision objects as hash tables that can carry values or functions. The prototype idea is just an associated hash table, and so on. The moment that I settled on this mental model, it was much easier to grok javascript.

Now, that is a vague explanation if I ever heard one, so let us speak in code:

public class Object
{
	private readonly Dictionary<string, Procedure> functions 
		= new Dictionary<string, Procedure>();
	private Object prototype;

	public Object Prototype
	{
		get
		{
			if(prototype==null)
				prototype = new Object();
			return prototype;
		}
		set { prototype = value; }
	}

	public Procedure this[string name]
	{
		get
		{
			if (functions.ContainsKey(name))
				return functions[name];
			if (prototype != null && prototype.functions.ContainsKey(name))
				return prototype[name];
			throw new InvalidOperationException("No function called " + name);
		}
		set { functions[name] = value; }
	}
}

This is a simple example of the matter, and here is how you can use it, if you ignore the fact that C# doesn't have any useful duck typing, then this code looks very much like the one that you would write in JavaScript:

Object obj = new Object();
obj["on_change"] = delegate { Console.WriteLine("changed"); };

obj.Prototype["on_load"] = delegate { Console.WriteLine("loaded from prototype"); };


obj["on_change"]();
obj["on_load"]();

//"overriding" the prototype method
obj["on_load"] = delegate{ Console.WriteLine("loaded from object"); };

obj["on_load"]();

obj["missing_method"]();//will throw