NPM Smell πŸ‘πŸ’¨

trivial and outdated NPM packages

get-func-name outdated npm logo

Convoluted way of accessing the .name property of a function.

Weekly Downloads
11,242,436
Dependencies
0
Latest Release
a year ago
This dependency re-creates a native JavaScript API. It is recommended to use the native API instead
Outdated
natively supported
since 11 years (chrome, firefox, safari, nodejs)

About

This dependency returns the name of a function.

You can get the name of a function by simply accessing the name property of the function.

It’s natively supported in JavaScript since well over 10 years by now.

Code

function f1() {}
f1.name; // "f1"

As a function

// returns name of the function or undefined if func is not a function
function getFuncName(func) {
    return func?.name;
}

More examples

const f2 = () => {};
f2.name; // "f2"

const f3 = function () {};
f3.name; // "f3"

const f4 = function f4() {};
f4.name; // "f4"

const f5 = new Function();
f5.name; // "anonymous"

class Foo {
    static f6 = () => {};
}
Foo.f6.name; // "f6"

class Bar {
    f7() {}
}
new Bar().f7.name; // "f7"

// even works with bound functions
const f8 = f1.bind({});
f8.name; // "bound f1"

// etc.