2

Good day,

Is there any alternative to arguments.callee.toString().match to find a function name ? Like make another function with a loop that will search for the name.

All my functions are named :

function mthdSearch() {
    console.log("test0")
};

function funkA() {
    console.log("test1");
}


function funkB() {
    console.log("test2");
}
8
  • Try to put all of them to an object and use Object.getOwnPropertyNames() Commented Jan 19, 2017 at 10:37
  • 2
    What exactly do you want to achieve? Take note, that arguments.callee is deprecated and removed in strict mode: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 19, 2017 at 10:38
  • You should be more specific about what you want to do. Do you want to test if a function for a given name exists? In this case—and in case there is a global object—globally declared functions are accessible from the global object. I.e. function f() {console.log("call to 'f'");} will be accessible on the window object (window.f() will result in a print to the console). Commented Jan 19, 2017 at 10:39
  • @FK82 , i'm a newbie in our company and i was given a task to find out if it is possible to search for function names and lets say write them in console.log . I have found arguments.callee , but u have to write it in each function ? And as Kuba told , it is deprecated in strict mode. Commented Jan 19, 2017 at 10:55
  • 1
    @KirillVorobjov From a general perspective, this is impossible because of scope. This is a counter example: jsfiddle.net/fqb8ukz5 . As you can see, if you are outside of the scope of the anonymous function, you cannot access the functions named f and h and thus cannot access their names. Commented Jan 19, 2017 at 11:58

1 Answer 1

1

As stated in my comment, this is impossible from a general perspective.

You can construct a counter example by declaring functions locally (i.e. within local scope) of another function and then try to access the declared functions by name outside of the scope of that function:

(function() {
	function f() {console.log("f")}
 	var g = function g() {console.log("g");};
 	h = function h() {console.log("h");};
  f();
  g();
  h();
})();

try {f();}
catch(e) {console.log(e.message);}

try {g();}
catch(e) {console.log(e.message);}

try {h();}
catch(e) {console.log(e.message);}

As you can see, only h is accessible outside of the scope of the anonymous function, because it was declared globally (by omitting the var keyword).


If you're looking for a technique to store a collection of functions and access them by name, use an object oriented JavaScript pattern (MDN introduction to object oriented JavaScript).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.