0

How could I find out all the function properties in JavaScript?

Here is what I tried:

var a = function(a, b, c) {};
a.length;
Object.keys(a);

enter image description here

So, the a definitely has the length property (with the value of 3), but for some reasons there is no "length" in the array produced by the Object.keys(a);. Why? How could I see all the function properties? I am interested to know both personal and inherited (using the proto) properties.

I am interested in this purely due to desire to learn and explore the intricacies of JavaScript. I do not have any real world application for this knowledge right now.

manymanymore
  • 2,251
  • 3
  • 26
  • 48
  • Did you tried to run `Object.keys(a).length`? – TzlilSwi Jul 07 '22 at 11:23
  • @TzlilSwi, just tried. It is zero as I would have expected. Or am I missing something here? The returned array is empty. – manymanymore Jul 07 '22 at 11:24
  • 2
    [Object.getOwnPropertyNames](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) gives you the non-enumerable properties too. – Teemu Jul 07 '22 at 11:24
  • What "function properties" are you looking for? [`Function.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) returns the number of parameters of the function. [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) returns the "own enumerable property names". Those are two different things. – Ivar Jul 07 '22 at 11:25
  • @Teemu, seems to be what I was looking for. Thanks. – manymanymore Jul 07 '22 at 11:26
  • "*I am interested in this purely due to desire to learn and explore the intricacies of JavaScript.*" Then this is [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) Don't just poke at it in the console, then. You might 1. not learn anything 2. not even learn the correct things 3. You might stumble upon implementation-specific things. Instead just read about this on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length – VLAZ Jul 07 '22 at 11:26
  • @VLAZ, I am not interested in the `length` property. I am interested to access all the function properties. I guess it is a process they don't write about on the MDN. – manymanymore Jul 07 '22 at 11:27
  • @manymanymore [you can find all function properties on MDN](https://i.stack.imgur.com/bOU5J.png). You're again asking for a Y. If you want to know *what* properties are there, what they do, and whether or not they are standard, then *read the documentation*. Poking at stuff in the console does not actually reveal any of this information. You only find out what properties are there, not if they are standard, or what they do. Or why. – VLAZ Jul 07 '22 at 11:29
  • @VLAZ, knowing what properties are there and knowing how to find them out are two different things. The skill of finding out properties of something can be used not only for functions, while the documentation you are sending me can be used only for functions. – manymanymore Jul 07 '22 at 11:36

0 Answers0