I am reading Javascript Web Applications by Alex Maccaw. Below is a snippet where a Class constructor is built.
var Class = function(parent){
var klass = function(){
this.init.apply(this, arguments);
};
// Change klass' prototype
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
};
klass.prototype.init = function(){};
// Shortcuts
klass.fn = klass.prototype;
klass.fn.parent = klass;
klass._super = klass.__proto__;
/* include/extend code... */
return klass;
};
The abit i'm interested in is
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
Why doesn't he go
klass.prototype = parent.prototype;
He explains it: "This little dance around creating a temporary anonymous function prevents instances from being created when a class is inherited". This still doesn't make sense to me, how would
klass.prototype = parent.prototype;
make instances be created?