fe-interview
fe-interview copied to clipboard
[js] 第304天 写一个方法检查给定的函数是否为js运行时环境的内置函数
没懂啥意思……
没懂啥意思……
就是,比如你在浏览器里执行js,那么history.go这个方法就是运行时环境的内置函数,区别于用户或者第三方库自定义的函数。
function isNativeFunc (func) { // 判断函数是否为运行时环境的内置函数
return /[native code]/.test(func)
}
function isNativeFunc(fun){
if(typeof fun !== 'function')
throw new Error('isNativeFunc参数类型必须为function');
return /\{\[nativecode\]\}$/.test(fun.toString().replace(/\s/g,''))
}
function isNativeFunc (func) { // 判断函数是否为运行时环境的内置函数 return /[native code]/.test(func) }
你这个正则不对,[应该使用\转义/\[native code\]/.test(func)
function isNativeFunc (func) { // 判断函数是否为运行时环境的内置函数 return /[native code]/.test(func) }你这个正则不对,
[应该使用\转义/\[native code\]/.test(func)
哪怕正则对了,我方法里只要包含这个字符串就被判定为内置函数了,是不是有点怪……
function isNative (Ctor) { return typeof Ctor === 'function' && /native code/.test(Ctor.toString()) }