Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a function name into a string

Tags:

javascript

I have a function from an object, lets say Object.MyFunc. I need to send this function name into another function like so: doSomething("Object.MyFunc");

any advise?

like image 408
Amir Avatar asked Mar 29 '09 21:03

Amir


People also ask

How do I convert a function to a string?

Code to Convert a Function to String To convert a function to string, use the toString() method of a function object.

How can I call a function given its name as a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

How do I turn a function into a string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

How do you read a function name in Python?

Method 1: Get Function Name in Python using function. func_name. By using a simple function property function, func_name, one can get the name of the function and hence can be quite handy for the Testing purpose and also for documentation at times.


1 Answers

This is an old question, but in ES6 this ability is provided via the Function.name property. The ECMAScript 2015 Language Specification defines how this property is added to a Function instance:

9.2.11 SetFunctionName (F, name, prefix)

The abstract operation SetFunctionName requires a Function argument F, a String or Symbol argument name and optionally a String argument prefix. This operation adds a name property to F by performing the following steps: ...

Unfortunately, this hasn't yet been implemented in all major browsers: It's listed here as not supported in all IE versions, and only partially supported in all other major browsers (with Edge 13 having the best, albeit still partial, support currently).

like image 174
RJ Cuthbertson Avatar answered Sep 20 '22 20:09

RJ Cuthbertson