JavaScript 内置方法速查表 你是否曾经发现自己在 LeetCode 问题或调试会话中,然后想:“等等……Map 又有什么方法?” 😅 别害怕——这里有一份终极动态备忘单,可以帮助你检查和记住 JavaScript 对象的所有内置方法!
🔍 动态检查方法 使用此行来记录类型的所有方法:
console.log(Object.getOwnPropertyNames(Type.prototype)); 替换Type为Array、Map、Set等。
🧱 1. 数组 console.log(Object.getOwnPropertyNames(Array.prototype)); [ "length", "constructor", "at", "concat", "copyWithin", "fill", "find", "findIndex", "flat", "flatMap", "includes", "indexOf", "join", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "toLocaleString", "toString", "values", "keys", "entries", "forEach", "filter", "map", "every", "some", "reduce", "reduceRight" ] 🔤 2. 字符串 console.log(Object.getOwnPropertyNames(String.prototype)); 🧰 3. 对象 console.log(Object.getOwnPropertyNames(Object.prototype)); 🗺️ 4. 地图 console.log(Object.getOwnPropertyNames(Map.prototype)); ["constructor", "clear", "delete", "get", "has", "set", "entries", "forEach", "keys", "values", "size"] 📦 5. 设置 console.log(Object.getOwnPropertyNames(Set.prototype)); 📆 6. 日期 console.log(Object.getOwnPropertyNames(Date.prototype)); 🔢 7. 数字 console.log(Object.getOwnPropertyNames(Number.prototype)); 🎯 8. 功能 console.log(Object.getOwnPropertyNames(Function.prototype)); 🧮 9. 数学(不是原型 - 但仍然有用) console.log(Object.getOwnPropertyNames(Math)); 🧬 10. JSON (也不是原型) console.log(Object.getOwnPropertyNames(JSON)); ✅ 奖励:创建您自己的检查员 function inspect(obj) { return Object.getOwnPropertyNames(Object.getPrototypeOf(obj)); }
console.log(inspect([])); // For Array console.log(inspect("hello")); // For String console.log(inspect(new Map())); 🧠 专业提示 其中许多方法包含不可枚举的方法,这对于全面探索非常有用。如果您只想获取可枚举的键:
console.log(Object.keys(Array.prototype)); 现在,您已经拥有了专属于您的浏览器 API 参考——就像调试器的手电筒一样使用它吧🔦。祝您编码愉快!以上内容由企业信息服务平台提供,致力于工商信用信息查询、企业风险识别、经营数据分析。访问官网了解更多:www.ysdslt.com