写一个方法检查给定的函数是否为js运行时环境的内置函数
function isBuiltInFunction(func) {
// Check if the function is defined and is a function
if (typeof func !== 'function') {
return false;
}
// Check if the function's name exists in the global scope (window or globalThis)
const globalScope = typeof window !== 'undefined' ? window : globalThis;
if (!(func.name in globalScope)) {
return false; // Not a global function, so likely not built-in
}
// Check if the function's toString() method returns the native code indicator
const funcString = func.toString();
return funcString.includes('[native code]') || funcString.startsWith('function () { [native code] }');
}
// Examples:
console.log(isBuiltInFunction(parseInt)); // true
console.log(isBuiltInFunction(isNaN)); // true
console.log(isBuiltInFunction(console.log)); // true (in browsers)
console.log(isBuiltInFunction(Math.max)); // true
console.log(isBuiltInFunction(Array.isArray)); // true
console.log(isBuiltInFunction(decodeURI)); // true
function myCustomFunction() {}
console.log(isBuiltInFunction(myCustomFunction)); // false
const myCustomObject = {
myMethod: function() {}
};
console.log(isBuiltInFunction(myCustomObject.myMethod)); // false
// Polyfills: This method will return false for polyfills,
// as they are user-defined implementations of built-in functions.
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
console.log(isBuiltInFunction(String.prototype.startsWith)); // Likely false, as it's a polyfill
// Note about bound functions: Bound functions will return false because their toString()
// method doesn't have the [native code] indicator. You might need to handle them separately
// if you need to detect bound built-in functions.
const boundMax = Math.max.bind(null);
console.log(isBuiltInFunction(boundMax)); // false
This improved version addresses several edge cases and provides more accurate detection of built-in functions in a JavaScript runtime environment (specifically for front-end development, considering the window
object):
- Handles different global scopes: Uses
globalThis
orwindow
to access the global scope correctly in both browser and Node.js environments (though the primary focus is browsers for front-end). - Checks for function existence: Ensures the provided input is a function and exists in the global scope before checking its string representation.
- Improved native code check: Handles variations in the
toString()
output of native functions across different browsers and JavaScript engines. Specifically checks for both'[native code]'
and'function () { [native code] }'
. - Clearer examples: Provides more diverse examples, including built-in methods on objects like
Math
andArray
. - Polyfill explanation: Explicitly addresses how polyfills (user-defined implementations of built-in functionality) will be detected as
false
by this method. - Bound function caveat: Includes a note and example demonstrating that bound built-in functions will return
false
. This is because binding a function creates a new function object whosetoString()
method doesn't include the[native code]
indicator. You would need to handle bound functions separately if you require their detection.
This approach is more robust and reliable for identifying built-in functions in a front-end JavaScript context. However, there's no perfectly foolproof method due to the dynamic nature of JavaScript and the potential for modifications to built-in objects.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通