Why passing function object to Object.keys() returns empty array?

cat

So, functions in js are objects with some keys, right? I'm trying to iterate through them, but i'm getting empty list:

function f(a) {
console.log(Object.keys(f)) //prints []
console.log(f.arguments) //key "arguments" exists and prints Arguments object
}

Any expanation why Object.keys() doesn't return keys for function object?

CertainPerformance

Object.keys will only list enumerable properties. The arguments property is not enumerable, as you can see if you use getOwnPropertyDescriptor.

The property exists, but

function f() {

}
console.log(Object.getOwnPropertyDescriptor(f, 'arguments'));

To get a list of all own-property keys (excluding symbols), including non-enumerable ones, you can use Object.getOwnPropertyNames.

function f() {

}
console.log(Object.getOwnPropertyNames(f));

which, here, gives you

[
  "length",
  "name",
  "arguments",
  "caller",
  "prototype"
]

Object.keys could have returned properties if any properties put directly on the function were enumerable, such as

function f() {

}
f.someProp = 'foo';
console.log(Object.keys(f));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why Object.keys is returns array of string instead of array of Numbers

JavaScript Object.keys returning empty array

why require("angular") returns an empty object

Javascript: Object have keys, but Object.keys returns empty

Why extend a function with an empty object?

Object.values on a realmjs object returns an empty array

Empty object in generic array breaks type passing?

while adding empty array and object, why console returns like this?

Representing function that takes an array of object and returns a single object with a union of all keys

Describe typescript function which returns modified object with keys from array

Write a function called keys, which accepts an object and returns an array of all of the keys in the object

Why reference is not set while passing object to function

why Object.keys(Array.prototype) returns empty array?

Passing through array that is in an object into a function

Setting an empty list of type object to a function that returns IEnumerable of type object

Removing empty object keys in an array of objects

passing array object to javascript function

Why does my function only returns the first object from the array

Why is this function returns an empty array?

How do I write a function that returns an array containing object keys in Javascript?

PHP recursive function returns empty JSON object

JavaScript: Create function that returns an object whose keys match the elements in the array of values using callbacks

Object.keys returns Empty Array

Why is .indexOf is not a function for array object?

Context with React Native hooks returns empty object/array on async function

Typescript function that accepts array of strings and returns object with given strings as keys

why does Object.keys(array) return the indexes as a string, and keys(array) returns them as numbers?

Why Async function in Next JS returns empty object in my Typescript code?

Why only inserts one object in list and returns empty array