JavaScript vs Ruby in Inheritance

Both JavaScript and Ruby have special inheritance model. In JavaScript, it is handled by stuff known as prototype chain; in ruby, there is a special class named as Singleton Class that wires all the magic up. The basic concept is the same: instance methods should be placed in ‘Class’ so that the instance can refer to it rather than duplicating it. Here is a diagram to clarify the inheritance chain in JS and Ruby and hope the comparison will shed some light on the common concept.

Note: the foo object in ruby diagram also has an corresponding singleton class. And the extend key word is a short-cut to open the singleton class.

moddule m
// here is the module
// do whatever you want 

foo.exend m
foo.instance_eval do
// here the singleton class of object foo is open 
// do whatever you want 


comments powered by Disqus