The problem you are suffering with is that the variables are not covered in blocks.
When your function starts, it updates the context prototype so that each function calls the same original function, which is the last element that belongs to the original prototype. In this case, it is webkitGetImageDataHD .
This means that when you call ctx.beginPath(); , you really call ctx.webkitGetImageDataHD(); . This method expects 4 arguments, and since it has not received them, it throws a DOM 9 exception.
Since JavaScript does not support the scope of the block, you need to force the scope to be changed using the function. By modifying your example, we can create a new function, where original is a fixed value:
var context = Object.getPrototypeOf(document.createElement('canvas').getContext('2d')); function bind(context, p) { // context, p, and original never change. var original = context[p]; context[p] = function(){ console.log(p, arguments); return original.apply(this,arguments); } } // p changes with every iteration. for(p in context){ if(context.hasOwnProperty(p)){ bind(context, p); } }
Find a working demo here: http://jsfiddle.net/bnickel/UG9gF/
source share