How to find hidden properties/methods in Javascript objects?

I would like to automatically determine all of the properties (including the hidden ones) in a given Javascript object, via a generalization of this function: function keys(obj) { var ll = []; for(var pp in obj) { ll.push(pp); } return ll; } This works for user defined objects but fails for many builtins: repl> keys({"a":10,"b":2}); // ["a","b"] repl> keys(Math) // returns nothing! Basically, I'd like to write equivalents of Python's dir() and help(), which are really useful in exploring new objects. My understanding is that only the builtin objects have hidden properties (user code [evidently][1] can't set the "enumerable" property till HTML5), so one possibility is to simply hardcode the properties of Math, String, etc. into a dir() equivalent (using the lists such as those [here][2]). But is there a better way? **EDIT:** Ok, the best answer I've seen so far is on [this thread][3]. You can't easily do this with your own JS code, but the next best thing is to use console.dir in Chrome's Developer Tools (Chrome -> View -> Developer -> Developer Tools). Run console.dir(Math) and click the triangular drill down to list all methods. That's good enough for most interactive/discovery work (you don't really need to do this at runtime). [1]: https://stackoverflow.com/questions/2636453/is-it-possible-to-create-a-hidden-property-in-javascript [2]: http://www.w3schools.com/jsref [3]: https://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object/2313745#2313745

以上就是How to find hidden properties/methods in Javascript objects?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏