Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want something to happen at the beginning of a function, you can use the following. You do have access to <code>this</code> and the arguments from your injected function. So it will still work for functions that require a specific context.</p> <pre><code>function inject(before, fn) { return function(){ before.apply(this, arguments); fn.apply (this, arguments); } } </code></pre> <p>For example</p> <pre><code>function add(a, b) { return a + b; } function concat(a, b) { return a + b; } /** * You can repeat index and type to check multiple arguments */ function createArgumentChecker(index, type /**index, type, ... */) { var originalArgs = arguments; return function() { for (var i=0; i &lt; originalArgs.length; i+=2) { var index = originalArgs[i], requestedType = originalArgs[i+1], actualType = typeof arguments[index]; if (typeAtIndex != actualType) { console.log("Invalid argument passed at index " + index + ". Expected type " + requestedType + "but it's " + actualType ); } } } } function logArguments() { console.log(this, arguments); } // Inject an argument checker add = inject(add, createArgumentChecker(0,"number", 1, "number")); concat = inject (concat, createArgumentChecker(0, "string", 1, "string")); // You can even do it multiple times, inject an argument logger; add = inject(add, logArguments); concat = inject(concat, logArguments); </code></pre> <p><em><a href="http://jsfiddle.net/sDBJ9/" rel="nofollow">JSfiddle</a></em></p> <p>This can be handy when debugging websites that you can't modify the source code, I wouldn't use it do parameter checking unless you can strip it our for the production version.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload