Menu

gitpiper

functions javascript Code Snippet in 2024

objectfunctionadvanced

Last Updated: 20 April 2024

Gets an array of function property names from own (and optionally inherited) enumerable properties of an object.

  • Use Object.keys(obj) to iterate over the object's own properties.
  • If inherited is true, use Object.getPrototypeOf(obj) to also get the object's inherited properties.
  • Use Array.prototype.filter() to keep only those properties that are functions.
  • 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');
function Foo() { this.a = () => 1; this.b = () => 2; } Foo.prototype.c = () => 3; functions(new Foo()); // ['a', 'b'] functions(new Foo(), true); // ['a', 'b', 'c']

javascript snippet similar to functions For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️