Function Property Names

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to use JavaScript to get an array of function property names from an object's own enumerable properties. We will learn how to iterate over an object's properties and use Object.keys() and Object.getPrototypeOf() to include inherited properties if needed. We will also filter out non-function properties using Array.prototype.filter().

How to Get Function Property Names from an Object in JavaScript

To get an array of function property names from an object, use the functions function provided below. This function can also optionally include inherited properties.

Here's how to use the functions function:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Object.keys() to iterate over the object's own properties.
  3. If you want to include inherited properties, set the inherited argument to true and use Object.getPrototypeOf() to get the object's inherited properties.
  4. Use Array.prototype.filter() to keep only those properties that are functions.
  5. Omit the second argument, inherited, to not include inherited properties by default.
const functions = (obj, inherited = false) =>
  (inherited
    ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))]
    : Object.keys(obj)
  ).filter((key) => typeof obj[key] === "function");

Here's an example usage of the functions function:

function Foo() {
  this.a = () => 1;
  this.b = () => 2;
}
Foo.prototype.c = () => 3;
functions(new Foo()); // ['a', 'b']
functions(new Foo(), true); // ['a', 'b', 'c']

Summary

Congratulations! You have completed the Function Property Names lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like