3

In python word there are dir() function which

return a list of valid attributes for that object

In JS words I found:

Object.getOwnPropertyNames

Object.keys

but they don't show ALL attributes:

> Object.getOwnPropertyNames([])
[ 'length' ]

How to get list of all attributes and methods as

concat, entries, every, find.... 

for Array() for example?

comalex3
  • 2,497
  • 4
  • 26
  • 47
  • 2
    Possible duplicate of [Is there a way to print all methods of an object in javascript?](http://stackoverflow.com/questions/152483/is-there-a-way-to-print-all-methods-of-an-object-in-javascript) – E.D. Mar 07 '17 at 20:52
  • 1
    @ErikBrodyDreyer—that question is not really a duplicate, since in 2008 there was no method to get non–enumerable property names. – RobG Mar 07 '17 at 21:01

4 Answers4

7

You can use Object.getOwnPropertyNames with Object.getPrototypeOf in order to traverse the prototype chain and collect all own properties on each object.

var result = []
var obj = []
do {
  result.push(...Object.getOwnPropertyNames(obj))
} while ((obj = Object.getPrototypeOf(obj)))

document.querySelector("pre").textContent = result.join("\n")
<pre></pre>

This handles all properties irrespective of whether or not they're inherited or enumerable. This does not include Symbol properties however. To include those, you can use Object.getOwnPropertySymbols.

var result = []
var obj = []
do {
  result.push(...Object.getOwnPropertyNames(obj), ...Object.getOwnPropertySymbols(obj))
} while ((obj = Object.getPrototypeOf(obj)))
  • Since the OP wants methods, there should be a filter for Function objects (or at least callable objects). – RobG Mar 07 '17 at 21:04
  • @RobG: Maybe. The OP mentions *"attributes and methods"*, so I'm not sure exactly how much or little should be included. –  Mar 07 '17 at 21:05
2

Object.getOwnPropertyNames(Array.prototype)

The reason why trying to get the values the way you posted does not work, is because you are requesting the property names for a single instance of the Array object. For a number of reasons, each instance will only have property values that are unique to that instance. Since the values found in Array.prototype are not unique to a specific instance -- which makes sense, not all arrays are going to share the same value for length -- they are shared/inherited for all instances of Array.

Pytth
  • 4,008
  • 24
  • 29
0

You could use Object.getOwnPropertyNames

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

console.log(Object.getOwnPropertyNames(Array.prototype));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

This method will allow you to pull all the keys and functions from a particular instance of an object (ignoring the unwanted ones):

const ROOT_PROTOTYPE = Object.getPrototypeOf({});

function getAllKeys(object) {
    // do not add the keys of the root prototype object
    if (object === ROOT_PROTOTYPE) {
        return [];
    }

    const names = Object.getOwnPropertyNames(object);

    // remove the default constructor for each prototype
    if (names[0] === 'constructor') {
        names.shift();
    }

    // iterate through all the prototypes of this object, until it gets to the root object.
    return names.concat(getAllKeys(Object.getPrototypeOf(object)));
}

Kevin Upton
  • 3,336
  • 2
  • 21
  • 24