fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

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

Open haizhilin2013 opened this issue 5 years ago • 7 comments

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

我也要出题

haizhilin2013 avatar Feb 13 '20 20:02 haizhilin2013

没懂啥意思……

wheatup avatar Feb 14 '20 06:02 wheatup

没懂啥意思……

就是,比如你在浏览器里执行js,那么history.go这个方法就是运行时环境的内置函数,区别于用户或者第三方库自定义的函数。

Yakima-Teng avatar Feb 14 '20 06:02 Yakima-Teng

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

Yakima-Teng avatar Feb 14 '20 06:02 Yakima-Teng

function isNativeFunc(fun){
	if(typeof fun !== 'function') 
		throw new Error('isNativeFunc参数类型必须为function');
	return /\{\[nativecode\]\}$/.test(fun.toString().replace(/\s/g,''))
}

yu1176887527 avatar Jul 03 '20 08:07 yu1176887527

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

你这个正则不对,[应该使用\转义/\[native code\]/.test(func)

wind8866 avatar Mar 25 '22 09:03 wind8866

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

你这个正则不对,[应该使用\转义/\[native code\]/.test(func)

哪怕正则对了,我方法里只要包含这个字符串就被判定为内置函数了,是不是有点怪……

wheatup avatar Apr 11 '22 02:04 wheatup

function isNative (Ctor) { return typeof Ctor === 'function' && /native code/.test(Ctor.toString()) }

xiaoqiangz avatar Sep 02 '22 03:09 xiaoqiangz