Fe-interview: [js] 第304天 写一个方法检查给定的函数是否为js运行时环境的内置函数

Created on 13 Feb 2020  ·  4Comments  ·  Source: haizlin/fe-interview

第304天 写一个方法检查给定的函数是否为js运行时环境的内置函数

我也要出题

js

Most helpful comment

function isNativeFunc (func) { // 判断函数是否为运行时环境的内置函数
    return /[native code]/.test(func)
}

All 4 comments

没懂啥意思……

没懂啥意思……

就是,比如你在浏览器里执行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,''))
}
Was this page helpful?
0 / 5 - 0 ratings