Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like a job for a wrapper function to me.</p> <p>Write a function that calls the other function, and put your code at the end of that (or wherever). Then always call your version instead.</p> <p>Sort of an adapter pattern. <a href="http://en.wikipedia.org/wiki/Adapter_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Adapter_pattern</a></p> <p>I believe you can hide the scope of the original function and name yours the same if you cannot modify calling code (doesn't work on some intrinsic functions like alert, but should work for library code). If that isn't an option, see if prototype will let you extend the object to add the functionality. <a href="http://phrogz.net/js/classes/ExtendingJavaScriptObjectsAndClasses.html" rel="nofollow">http://phrogz.net/js/classes/ExtendingJavaScriptObjectsAndClasses.html</a></p> <pre><code> //Only add this implementation if one does not already exist. if (Array.prototype.slice==null) Array.prototype.slice=function(start,end){ if (start&lt;0) start=this.length+start; //'this' refers to the object to which the prototype is applied if (end==null) end=this.length; else if (end&lt;0) end=this.length+end; var newArray=[]; for (var ct=0,i=start;i&lt;end;i++) newArray[ct++]=this[i]; return newArray; } </code></pre> <p>For arguments, you can also make your version take optional arguments (similar to jQuery / MooTools) and then look at what was passed.</p> <p>Examples of optional arguments here. <a href="http://www.openjs.com/articles/optional_function_arguments.php" rel="nofollow">http://www.openjs.com/articles/optional_function_arguments.php</a></p> <pre><code>function accident() { for( var i = 0; i &lt; arguments.length; i++ ) { alert("This accident was caused by " + arguments[i]); } } </code></pre>
 

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