Short answer:
- No, you cannot get the "property name" used to call your function.
- There can be no name at all or several names in different areas, so the "property name" is rather poorly defined.
arguments.callee deprecated and should not be used.- There is no solution that does not use arguments or closure.
Long answer:
As the commentary commented, you should rethink what you are trying to do and ask for it instead in a new question. But there are some common misconceptions, so I will try to explain why you cannot get a "simple property name".
The reason is that it is not easy.
Before moving on, let's clarify something. Activation objects are not objects. The ECMAScript 5.1 specification calls them Environment Records (10.2.1) , but the more general term is Scope chain . In the browser, the global scope is ( often ) a window object, but all other areas are not objects. There may be an object that you use to call the function, and when you call the function, you should be in some area. They are different.
And there are many names.
- When you call a function, you need to reference it, for example, through an object property. This link may have a name.
- A chain of objects has declarations that always have a name.
- A
Function (a real function, not a link) can also have a function name - your arguments.callee.name - which is fixed in the declaration.
Not only different names, they are not always (always) the "name of the property" you are looking for.
var obj = { prop : function f(){} }, func = obj.prop; // "obj" and "func" are declarations. // Function name is "f" - use this name instead of arguments.callee // Property name is "prop" func(); // Reference name is "func" obj.prop(); // Reference names are "obj" and "prop" // But they are the same function! // PS "this" in f is undefined (strict mode) or window (non-strict)
Thus, a function reference can come from binding (e.g., function declarations), Object (arguments.callee), or variable . All of them are References (8.7) . And the link has a name (so to speak).
A function reference trap does not always come from an object or chain of scopes, and its name is not always determined. For example, a general closure method:
(function(i){ })(i)
Even if the link has a name, a function call (11.2.3) does not pass the link or its name to the function in any case. This ensures the functionality of the JavaScript engine. Consider this example:
eval("(new Function('return function a(){}'))()")() // Calls function 'a'.
The final function call refers to the eval function, which refers to the result of a new global scope (in strict mode , anyway), which refers to a function call statement that refers to a group that refers to an anonymous Function object, and which contains code that expresses and returns a function named "a".
If you want to get the "property name" from inside a, which should it get? "Eval"? "Function"? "Anonymous"? "A"? All of them? Before you answer, consider such difficulties as accessing functions through iframes, which has different global variables, as well as the restriction on crossing crosses or interacting with native functions (for example, Function.prototype.bind ), and you will see how he quickly turns into hell.
That is why arguments.caller , __caller__ and other similar methods are now deprecated. The "property name" of a function is even more poorly defined than the caller, which is almost unrealistic. At the very least, the caller is always the execution context (optionally a function).
So, not knowing what your real problem is, the best way to get the “property name” is to use closure.