quirkey/sammy — Issue #130: Defining prototypes to allow inheritance
Author: GilesBradshaw
Created: 2012-03-07 00:08:24+00:00
State: open

I've been writing some coffeeScript to inherit one of the classes in Sammy like this:

class DataLocationProxyTest extends Sammy.DataLocationProxy
setLocation : (newLocation) ->
super newLocation

With the way DataLocationProxy is written this doesn't work because the constructor is not inherited. To be inherited DataLocationProxy.prototype.constructor would have to be the DataLocationProxy function.

This is because the prototype is defined as a new object :

Sammy.DataLocationProxy.prototype = {
bind: function() {
var proxy = this;
this.app.$element().bind('setData', function(e, key, value) {
if (key == proxy.data_name) {
......

if the prototype is defined like this :
Sammy.DataLocationProxy.prototype.bind = function() {
var proxy = this;
.....
Sammy.DataLocationProxy.prototype.unbind = function() {
if (this.href_attribute) {

&c then it is possible to inherit

Is there any reason that the prototypes are defined as they are and would it be better to extend rather than redefine them so that classes can be inherited? I note in other places it uses $.extend which does not cause this problem.