title: How to determine that a JavaScript function is native (without testing '[native code]')
body: <p>I would like to know if there is a way to differentiate a JavaScript script function (<code>function(){}</code>) from a JavaScript native function (like <code>Math.cos</code>).<br>
  I already know the <code>func.toString().indexOf('[native code]') != -1</code> trick but I was wondering if there is another way to detect it.  </p>
  
  <p>context:<br>
  I need to create a No-op forwarding ES6 Proxy that can handle native functions on an object but it fails with <code>TypeError: Illegal invocation</code> (see <a href="https://stackoverflow.com/questions/42496414/illegal-invocation-error-using-es6-proxy-and-node-js">Illegal invocation error using ES6 Proxy and node.js</a>).</p>
  
  <p>To workaround this I <code>.bind()</code> all my functions in the <code>get</code> handler of my Proxy but if I could detect native function effectively, I will only need to <code>.bind()</code> these native functions.</p>
  
  <p>more details: <a href="https://github.com/FranckFreiburger/module-invalidate/blob/master/index.js#L106" rel="nofollow noreferrer">https://github.com/FranckFreiburger/module-invalidate/blob/master/index.js#L106</a></p>
  
  <p>note:</p>
  
  <pre><code>(function() {}).toString() -&gt; "function () {}"
  (function() {}).prototype  -&gt; {}
  
  (require('os').cpus).toString() -&gt; "function getCPUs() { [native code] }"
  (require('os').cpus).prototype  -&gt; getCPUs {}
  
  (Math.cos).toString() -&gt; "function cos() { [native code] }"
  (Math.cos).prototype  -&gt; undefined
  
  (Promise.resolve().then).toString() -&gt; "function then() { [native code] }"
  (Promise.resolve().then).prototype  -&gt; undefined
  </code></pre>
  
  <p><strong>edit:</strong><br>
  For the moment, the best solution is to test <code>!('prototype' in fun)</code> but it will not work with <code>require('os').cpus</code> ...</p>