Why are arguments.callee.caller.name undefined?

Why doesn't this warn "http://127.0.0.1/sendRequest"? (Available http://jsfiddle.net/Gq8Wd/52/ )

var foo = { sendRequest: function() { alert(bar.getUrl()); } }; var bar = { getUrl: function() { return 'http://127.0.0.1/' + arguments.callee.caller.name; } }; foo.sendRequest(); 
+1
source share
4 answers

Putting a value in an object literal, as you do, does not affect the value at all.

 var foo = { sendRequest: ... 

The value of a function is affected only by a function expression that does not contain a name.

  ... function() { alert(bar.getUrl()); } 

You need to specify the name that you want in the expression of the [ fiddle ] function itself.

 var foo = { sendRequest: function sendRequest() { 
+2
source

If you do this:

 var foo = { sendRequest: function() { alert(bar.getUrl()); } }; var bar = { getUrl: function() { return arguments.callee; } }; foo.sendRequest(); 

You will notice that the function does not have a name that is true:

 function() { 

This is an anonymous feature.

You can name your method: sendRequest: function myMethodName() {

+2
source

Although the function is stored under the property of the foo.sendRequest object and therefore can be called via foo.sendRequest() , this function itself does not have a name. Therefore, arguments.callee.caller.name empty.

+2
source

Because the function that calls the called function is an anonymous function (and therefore does not have a name).

Try:

 function sendRequest() { alert(bar.getUrl()); } var foo = { sendRequest: sendRequest }; var bar = { getUrl: function() { return 'http://127.0.0.1/' + arguments.callee.caller.name; } }; foo.sendRequest(); 
0
source

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


All Articles