2

So, I'm trying to get some traction with prototypes in JavaScript, and I started by trying something basically like the second example in Understanding prototypal inheritance in JavaScript. Currently, I have a bunch of functions that, were I writing in C++, would certainly like to inherit from a parent class; imagine a bunch of functions like

function specificDevice(){
     this.codeNumber = 'abc123';
     this.wrapperDiv = window.parameters.dashboardDiv;
}

I have a whole pile of these functions, and everything worked fine - but the code was kind of silly, since they all shared a lot of members cut and pasted from one to the next. So, per the link above, I did something like

function generalDevice(){
     this.codeNumber = 'abc123';
}

specificDevice.prototype = new generalDevice();

function specificDevice(){
     this.wrapperDiv = window.parameters.dashboardDiv;

     //just to check if this does what I think it does:
     alert(this.codeNumber);
     alert(this.wrapperDiv);
}

And when I make a new instance of specificDevice, the alerts spat out exactly what I expected - 'abc123', and whatever I previously defined as window.parameters.dashboardDiv - perfect! But then, I tried to do what I figured would be the exactly analogous thing on the member variable being pulled from the window object; the above now becomes:

function generalDevice(){
     this.codeNumber = 'abc123';
     this.wrapperDiv = window.parameters.dashboardDiv;
}

specificDevice.prototype = new generalDevice();

function specificDevice(){
     alert(this.wrapperDiv)
}

Disaster - the script falls over with a

'Cannot read property "dashboardDiv" of undefined'.

I'm hoping the problem is obvious to the experts - how come I can make an object that happily recognizes my window.parameters as a thing, but that object's prototype thinks window.parameters doesn't exist?? Thanks in advance.

Community
  • 1
  • 1
Bill Mills
  • 411
  • 6
  • 16
  • Why do you use `window` in the first place? Why don't you use just `parameters`? JavaScript will resolve the scope (assuming it's a unique variable name) – elclanrs Mar 29 '13 at 22:22
  • 1
    Seems to be a simple matter of execution order. You're presumably invoking `generalDevice()` before you've defined `window.parameters`. This may be happening while you're defining the `.prototype` of `specificDevice`. You can create an inherited object without invoking the constructor that holds the prototype by using `Object.create`. So... `specificDevice.prototype = Object.create(generalDevice.prototype);` –  Mar 29 '13 at 22:30
  • @elclanrs : sure, but the same problem persists. – Bill Mills Mar 29 '13 at 22:47
  • @amnotiam : ...yes, I think you're right! The undefined window.parameters thing goes away when I do it your way; unfortunately it's then replaced with a different problem (specificDevice then can't fetch *anything* from the generalDevice prototype) - but I think that's a different problem, seeing as what you said about generalDevice getting called immediately is correct. Thanks very much! – Bill Mills Mar 29 '13 at 22:49
  • You're welcome. And to your new issue, I would guess that you may be invoking `specificDevice` before you've assigned its new `.prototype` object. –  Mar 29 '13 at 22:55
  • @amnotiam - I didn't think so, but I'm not going to insist; the problem goes away however, if I do like the mozilla folks under their Object.create example, and do generalDevice.call(this) at the top of specificDevice - then everything is perfect. Thanks for pointing out Object.create()! – Bill Mills Mar 29 '13 at 23:24
  • @user1807834: That's absolutely correct. I failed to notice that you weren't applying the `generalDevice` constructor logic to the `specificDevice` object. I was too focused on the prototype. The Mozilla solution is correct. –  Mar 29 '13 at 23:31
  • Object.create should also work, without .call, if you simply move codeNumber and wrapperDiv to generalDevice.prototype (since that's what you're extending if you use the code provided by @amnotiam). – bfavaretto Mar 30 '13 at 00:58

0 Answers0