0

With this organisation, is it possible to reference the "id" property?

  function house(id) {  this.id = id }

  house.prototype.buy  = function() {  }
  house.prototype.sell = function() {  }

  house.prototype.buy.makeOffer=function(){
    //how can id be printed?
    console.log( 'id = ' + this.????  )      
  }

  var h = new house(1)
  h.buy.makeOffer();

I am trying to figure out the best way to organize a lot of prototypes for an object. As .buy will have a lot of sub-function an so will .sell() ie:

  h.buy.makeOffer();
  h.buy.inspect();
  h.buy.counterOffer();
  h.buy.signContact();

  h.sell.prepare()
  h.sell.advertise()
  h.sell.reviewOffer()
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57
Brian McGinity
  • 5,777
  • 5
  • 36
  • 46
  • 2
    `.buy.makeOffer` doesn't use prototype at all. – thefourtheye Mar 26 '15 at 00:29
  • 1
    Be sure to double-check what `this` references in each place you use it. – Etheryte Mar 26 '15 at 00:31
  • Actually, this is a better duplicate: http://stackoverflow.com/q/15884096/218196 – Felix Kling Mar 26 '15 at 00:35
  • You're probably after a classical inheritance language. Douglas Crockford [wrote](http://www.crockford.com/javascript/inheritance.html) on this subject. – shennan Mar 26 '15 at 00:46
  • The answer posted here http://stackoverflow.com/questions/15884096/organize-prototype-javascript-while-perserving-object-reference-and-inheritance looks like it defeats the reason for using prototypes as the each object duplicates the methods. – Brian McGinity Mar 26 '15 at 00:50

1 Answers1

1

It's really not very easy to use a structure like this in Javascript:

house.prototype.buy.makeOffer=function(){
    //how can id be printed?
    console.log( 'id = ' + this.????  )      
}

The issue is that when you call that method on an instance of house like this:

var h = new house(1);
h.buy.makeOffer();

the this value in the makeOffer() method will point to the buy object in the prototype (which is your buy method) and it will NOT point to your house object. That's just how method calls work in Javascript (this points to the object which had the method) which renders these nested methods when there's actual instance data that you want to reference via this very impractical.


The usual solution is to not implement the nesting. Just integrate the names without an extra layer of objects like this:

house.prototype.buyMakeOffer=function(){
    //how can id be printed?
    console.log( 'id = ' + this.????  )      
}

var h = new house(1);
h.buyMakeOffer();
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • This solution is the only way I can see to do it. Really I was hoping for a more organized solution. – Brian McGinity Mar 26 '15 at 00:52
  • @BrianMcGinity - there are ways to try to work around this issue, but IMO, they create far more complication than they justify. You can just use a common prefix with an underscore `h.buy_makeOffer()` and get the same namespace separation as you get with the dot nomenclature so it's not actually a practical issue at all - just one that you like the way it looks better with the dot nomenclature. But, unfortunately, Javascript makes that way messy. – jfriend00 Mar 26 '15 at 01:07