How to find hidden properties/methods in Javascript objects?

后端 未结 4 1101
难免孤独
难免孤独 2020-12-18 15:05

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:



        
相关标签:
4条回答
  • 2020-12-18 15:23

    Object.getOwnPropertyNames won't return "the hidden ones".
    Object.getOwnPropertyNames returns the names of non-inherited properties.

    0 讨论(0)
  • 2020-12-18 15:31

    this works in firebug to find object methods.

    Object.getOwnPropertyNames(Math);

    0 讨论(0)
  • 2020-12-18 15:36

    ECMAScript 5th ed. defines Object.getOwnPropertyNames that returns an array of all properties of the passed in object, including the ones that are non-enumerable. Only Chrome has implemented this so far.

    Object.getOwnPropertyNames({a: 10, b: 2});
    

    gives ["b", "a"] (in no particular order)

    Object.getOwnPropertyNames(Math);
    

    gives ["LN10", "PI", "E", "LOG10E", "SQRT2", "LOG2E", "SQRT1_2", "LN2", "cos", "pow", "log", "tan", "sqrt", "ceil", "asin", "abs", "max", "exp", "atan2", "random", "round", "floor", "acos", "atan", "min", "sin"]

    0 讨论(0)
  • 2020-12-18 15:37

    This is explained in a previous answer. Basically, the spec explicitly requires (using DontEnum) that these objects aren't enumerable.

    0 讨论(0)
提交回复
热议问题