0

I am trying to make the javascript "constructor" call one of it's prototypical methods. Is it possible to do so?

Here is the code:

    function updateableTable($table, $rowTemplate,  dataObjects, getIDFromObject) // at some point add the ability to add event handlers.
{
    this.$rowTemplate = $rowTemplate;
    this.$tableBody = $j($table).children('tbody');
    this.rowsByID = {};
    this.dataObjectsByID = {};
    this.getIDFromObject = {};

    for(var dataObj in dataObjects)
    {
        addRow(dataObj);
    }

    return {}; // still has the protypical methods.
}

updateableTable.prototype.addRow = function(dataObject) {
    //alert('inside add row');
    var id = this.getIDFromObject(dataObject);
    // if (true === doesIdExist(i)) {
        // alert('id ' + id + ' already exists in table.');
        // return false;
    // }
    var newRow = $rowTemplate.tmpl(dataObject);
    this.rowsByID.add(id, newRow);
    this.dataObjectsByID(id, dataObject);
    this.$tableBody.append(newRow);
    return true;
}

it references jQuery and jQuery.tmpl (just a way of converting data to the view of the data, and not really important for this question) and I call it like this:

var $j = jQuery.noConflict();
var peeps = [{ID:1, Name:'mary', Age:21},{ID:2,Name:'contrary', Age:12}];
var personIDGetter = function(person)
{
    return person.ID;
}
var theTableUpdater = updateableTable($j('table.updateable'), 'hi', peeps, personIDGetter);

But it tells me (prototypical method) addRow doesn't exist.

Am I going about this all wrong?

UPDATE: Yes, I did many things wrong. I added a jsfiddle with the suggested fixes as well as a number of other fixes. It adds two rows and deletes one.

user420667
  • 6,552
  • 15
  • 51
  • 83
  • 1
    Yep, just this.addRow() should work. Why not make a jsfiddle and try it out there? – Michael Chaney Sep 15 '14 at 21:58
  • *"I am trying to make the javascript "constructor" call one of it's prototypical methods. Is it possible to do so?"* You just call it, the prototype is hooked up before the constructor (no quotes) is called. – T.J. Crowder Sep 15 '14 at 21:59
  • 1
    *"`return {}; // still has the protypical methods.`"* No, it doesn't. Or rather, it does, but only the ones from `Object.prototype`, not yours. – T.J. Crowder Sep 15 '14 at 22:00
  • 1
    *"`var theTableUpdater = updateableTable(...)`"* does nothing to create a new object that uses `updateableTable.prototype`. For that, you need `new` (or `Object.create`). In your code as shown, within the call to `updateableTable,` `this` is the global object (the object also referenced by `window` on browsers). – T.J. Crowder Sep 15 '14 at 22:01
  • oh man, so many mistakes. Thanks for the code review folks. – user420667 Sep 15 '14 at 22:02
  • Maybe the following information can help you, not with this specific problem maybe but in general: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Sep 16 '14 at 11:53

0 Answers0