Access function (.caller) properties without specifying its own name

In JavaScript, you can access functional properties functions, for example caller. (In fact, I don’t even know if the “functional property” is the right word.)

For instance:

func0 = function() {
   console.log(typeof func0.caller)
}
func1 = func0
func1()
var o = { func2: func0, func3: function(){ console.log(typeof o.func3.caller) } }
o.func2()
o.func3()

As you can see, before adding .caller, you must specify the name of the function. But if the function is anonymous or for some reason I don’t want to use the name (maybe I plan to rename fucntion in the future): can I access the caller?

+4
source share
1 answer

, , - , . , . arguments.

arguments , [0] , .

arguments.length 
// a property of the arguments object that tells you how many arguments the function has

arguments.caller
// reference to the function that invoked the current function.

arguments.callee() will call the function recursively. Its a reference to the currently executing function.

, ?

Use arguments.callee.caller

, , arguments.callee , arguments.caller , ( ). , arguments.caller .

+2

Source: https://habr.com/ru/post/1536037/


All Articles