0

I'm still trying to wrap my brain around JavaScript's "inheritance" model so forgive me if this is question is simply answered by pointing to JS docs.

I basically want to pass in my own custom and inherited Image object to the drawImage method on the canvas context. The end goal is to be able to store additional render data in the derived class, and while there are alternatives to store the additional data, I thought an inheritance implementation might be cleaner in this instance.

Is there a way to get the derived type to render?

function MyImage() {}
MyImage.prototype = new Image();
...
var test = new MyImage();
test.src = "myimage.jpg";
test.onload = function() { context.drawImage(test,0,0); }; //Fails to render
...
var test2 = new Image();
test2.src = "myimage.jpg";
test2.onload = function() {context.drawImage(test2,0,0);}; //Renders correctly
rysama
  • 1,674
  • 16
  • 28
  • I highly doubt that there is a way to make your own native JS objects 'inheriting' from native host objects be treated as host objects for host commands like `drawImage()`. Sounds like a neat idea, though. (Posted as a comment and not answer since I cannot say with certainty that it is not possible.) – Phrogz Nov 23 '11 at 22:39
  • Inheriting from host objects is quite a large can of worms at this point. Take a look at my earlier answer to a similar question — http://stackoverflow.com/q/1494316/130652 – kangax Nov 24 '11 at 06:13
  • Possible duplicate of [How to inherit from the DOM element class](http://stackoverflow.com/questions/1489738/how-to-inherit-from-the-dom-element-class) – Paul Sweatte May 16 '17 at 21:37

0 Answers0