I'm trying to define a base class in JavaScript that performs a lot of common functionality upon creation. Part of that functionality is to create a component and register callback handlers to that component.
The problem I'm having is how to override the function that's being used for that callback handler in child classes that extend my base class.
In concrete terms I have a BasicPage
component that creates a Table
component. The BasicPage
also defines a default onRowClick
function that gets registered with the newly created Table
.
Now I want to create a PageWithSections
component that extends BasicPage
(via a call(..)
statement) and overrides onRowClick
. The problem is the registration of the click handler with the table happens within the constructor of the base class. At the time of that registration, onRowClick
hasn't been overridden yet, so the reference is to the base classes version of onRowClick
.
I've made a jsBin that illustrates the problem.
http://jsbin.com/uDEjuzeQ/9/edit
If you click on each box once, in order, I want the message box display to be:
No messages yet; row clicked; row clicked; BasicPage onRowClicked; row clicked; PageWithSections onRowClicked
What is the proper way to override a function up the constructor chain and bind the overridden function to something during construction of a base object?
UPDATE This question original referenced a prototype chain, but in truth the prototypes are not actually being used in this example.
The question was updated to reflect that. This ends up being more of a question about late binding.
My co-worker came up with one possible solution. As @cloudfeet said, it's not prototypal, but it works.
Basically he set the binding to a different instance function that in turn, called the
_onRowClick
function which at the time of execution would have been overridden.http://jsbin.com/uDEjuzeQ/16/edit