I would like to know if there is a way to differentiate a JavaScript script function (function(){}
) from a JavaScript native function (like Math.cos
).
I already know the func.toString().indexOf('[native code]') != -1
trick but I was wondering if there is another way to detect it.
context:
I need to create a No-op forwarding ES6 Proxy that can handle native functions on an object but it fails with TypeError: Illegal invocation
(see Illegal invocation error using ES6 Proxy and node.js).
To workaround this I .bind()
all my functions in the get
handler of my Proxy but if I could detect native function effectively, I will only need to .bind()
these native functions.
more details: https://github.com/FranckFreiburger/module-invalidate/blob/master/index.js#L106
note:
(function() {}).toString() -> "function () {}"
(function() {}).prototype -> {}
(require('os').cpus).toString() -> "function getCPUs() { [native code] }"
(require('os').cpus).prototype -> getCPUs {}
(Math.cos).toString() -> "function cos() { [native code] }"
(Math.cos).prototype -> undefined
(Promise.resolve().then).toString() -> "function then() { [native code] }"
(Promise.resolve().then).prototype -> undefined
edit:
For the moment, the best solution is to test !('prototype' in fun)
but it will not work with require('os').cpus
...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…