title: Intercepting function calls in javascript
body: <p>I appreciate if anyone can tell me how to intercept a function call in javascript.
  I know it is possible with making use of proxies.</p>
  
  <p>for example I tried the code below to intercept it but now I want to intercept <code>toDataURL()</code>. in order to call toDataURL you need to create a canvas element first.So, now I want to know how is this possible to define a proxy to intercept <code>toDataURL()</code>.</p>
  
  <p>Example code to intercept it :</p>
  
  <pre><code>window.x = 0;
      let calls = (function(){
          let canvas = document.createElement('canvas');
          let fun = canvas.toDataURL;
          canvas.toDataURL = function(){       
              window.x++;         
              return fun.apply(document, arguments);
          }
          return ()=&gt;calls;
      })();
  </code></pre>