views:

82

answers:

1

I've got a function:

function createOrLoadDB (host) {                          
  var db = JSON.parse( window.localStorage.getItem(host) )
  if ( db == null ) {                                     
    db = new InitDB(host)                                 
  }                                                       
  else {                                                  
    db.__proto__ = InitDB.prototype                       
  }                                                       
  return db                                               
}                                                         

That to me seems like it would work, but when I call db.flushDB() I get

TypeError: Object #<an InitDB> has no method 'flushDB'

Which is funny, because I've got that in my object def:

function InitDB ( host ) {
    ... stuff
    this.flushDB = function () {                                      
      window.localStorage.setItem( this.host, JSON.stringify( this ) )
    }                                                                 
    ... stuff
}

Am, I missing something. The __proto__ made it say #<an InitDB>, but it still isn't picking up the methods...

+1  A: 

Add your flushDB method to InitDB.protoype. Otherwise the method will only appear in objects explicitly created by InitDB.

Something like

function InitDB(host) {
    // the init stuff here, minus this.flushDB
}

InitDB.prototype.flushDB = function() {
    window.localStorage.setItem(this.host, JSON.stringify(this));
};
cHao
I thought this was exactly the type of stuff `.__proto__ = Obj.prototype` was supposed to save us from.. So, I have to do this for all of my methods?
Evan Carroll
`.__proto__ = Obj.protoype` was meant to save you from exactly what you're doing -- adding a method to every `Obj` when creating it. Instead, you put the stuff in `Obj.prototype`, and it becomes available in every object whose prototype is set to `Obj.prototype`. For any method you want available to objects that haven't been constructed by your constructor, yes -- you have to add them to the prototype, or make them regular functions that take the object as an argument.
cHao
Also keep in mind: not all browsers support `__proto__`. IE doesn't, IIRC.
cHao
do they still have to be methods on the object if they're in the `__proto__` ?
Evan Carroll
Once they're in `__proto__`, they *become* methods on the object, since they get inherited from the prototype.
cHao
Is there anyway to do this inside of the constructor function? (define the methods on the objects prototype) -- it seems rather disorganized having them outside of the constructor.
Evan Carroll
`if (typeof(InitDB.prototype.flushDB) != "function") { /* add the prototype function */ }` is the only way that immediately pops into mind. It's not really that disorganized to have them outside, though -- it's a pretty common thing, and is more organized than having them *inside* the constructor, since methods added inside the constructor won't exist until you call the constructor. In your case, with objects being loaded from storage, that could very well end up being something to worry about.
cHao
Thanks for all your help, I made you answer far to much before choosing this. ;)
Evan Carroll