Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an updated answer</p> <pre><code>var newFunc = oldFunc.bind({}); //clones the function with '{}' acting as it's new 'this' parameter </code></pre> <p>However ".bind" is a modern ( >=iE9 ) feature of JavaScript (with a compatibility workaround from MDN)</p> <p><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind" rel="noreferrer">https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind</a></p> <p><strong>Note:</strong> that it <strong>does not clone</strong> the function object additional attached <strong>properties</strong>, <strong>including</strong> the <strong>prototype</strong> property. Credit to <a href="https://stackoverflow.com/users/554406/">@jchook</a></p> <p><strong>Note:</strong> that the new function <strong>this</strong> variable is stuck with the argument given on bind(), even on new function apply() calls. Credit to <a href="https://stackoverflow.com/users/700980/">@Kevin</a></p> <pre><code>function oldFunc() { console.log(this.msg); } var newFunc = oldFunc.bind( { msg:"You shall not pass!" } ); // this object is binded newFunc.apply( { msg:"hello world" } ); //logs "You shall not pass!" instead </code></pre> <p><strong>Note:</strong> binded function object, instanceof treats newFunc/oldFunc as the same. Credit to <a href="https://stackoverflow.com/users/59272">@Christopher</a></p> <pre><code>(new newFunc()) instanceof oldFunc; //gives true (new oldFunc()) instanceof newFunc; //gives true as well newFunc == oldFunc; //gives false however </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