We can modify the DOM element and add its prototype. For example, if we want to add something only to the canvas, we would do something like this:
HTMLCanvasElement.prototype.doSomething = function(arg) { ... };
Then we can perform this action for the canvas element:
var canvas = document.getElementById('canvasId'); canvas.doSomething(...);
Is it possible to add / attach a function to this canvas instance without changing the prototype HTMLCanvasElement. I just want the canvas where doSomething (...) was called to have access to additional methods, and not to all elements of the canvas in the DOM. How can i do this?
I tried the following in my doSomething function:
this.prototype.foobar = function() {...}
However, the prototype is not defined here.
javascript dom html html5 javascript-events
Sai
source share