The Fork.extend function is easiest to first understand in terms of classical inheritance with super classes and sub classes like in Java or Ruby. Please read Kevin Lindsey's tutorial for more information. What Kevin calls baseConstructor I call superConstructor. What Kevin calls superClass I call superPrototype. I think these changes better denote the JavaScript nature of constructor chaining.
Some would say that using the extend function is simulating class-based inheritance in JavaScript and goes against the fundamental nature of the language. However, all the extend function really does is create some references from one function to another which is perfectly natural in JavaScript. I have found that I use extend very infrequently in JavaScript programming. The one place in Fork where extend is used so far is in the drag proxies where it is very useful.
The JavaScript language does not have classes. Just because two objects are created by the same constructor it does not mean they are similar. This because properties of one or both objects could have been changed, removed or modified. In this case their likeness may have been destroyed and thinking of them as in the same class is no longer valid.
Although FORK.extend() is supported by very old browsers it is likely that you may use Function.apply() or Function.call() in conjunction with FORK.extend(). In some not-as-old browsers (eg Internet Explorer 5.0) either one or both of Function.apply() or Function.call() may not exist and cause a runtime error. If you plan on using these functions you may be wise to test for their existence first.
FORK.extend = function(sub, sup) {
function F() {}
F.prototype = sup.prototype;
sub.prototype = new F();
sub.prototype.constructor = sub;
sub.superConstructor = sup;
sub.superPrototype = sup.prototype; // not necessary but nice convenience
};
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.toString = function() {
return this.first + " " + this.last;
};
function Employee(first, last, id) {
Employee.superConstructor.call(this, first, last);
this.id = id;
}
FORK.extend(Employee, Person);
Employee.prototype.toString = function() {
return Employee.superPrototype.toString.call(this) + ": " + this.id;
};