admin管理员组文章数量:1429071
How e this doesn't alert "http://127.0.0.1/sendRequest"? (Available at /)
var foo = {
sendRequest: function() {
alert(bar.getUrl());
}
};
var bar = {
getUrl: function() {
return 'http://127.0.0.1/' + arguments.callee.caller.name;
}
};
foo.sendRequest();
How e this doesn't alert "http://127.0.0.1/sendRequest"? (Available at http://jsfiddle/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();
Share
Improve this question
edited Nov 3, 2011 at 17:48
Trump Voters Deserve Damnation
130k88 gold badges358 silver badges381 bronze badges
asked Nov 3, 2011 at 17:44
user979672user979672
1,8433 gold badges23 silver badges32 bronze badges
3
- what you try to acheive ??????????? – Qchmqs Commented Nov 3, 2011 at 17:46
-
1
Because you have no function with name
sendRequest
, only anonymous functions. – Felix Kling Commented Nov 3, 2011 at 17:46 -
3
Even if
callee
andcaller
e in quite handy sometimes, those are deprecated since ES5-strict. So I'd remend not to use them anymore. – jAndy Commented Nov 3, 2011 at 17:49
4 Answers
Reset to default 2Putting a value in an object literal, as you're doing, doesn't affect the value at all.
var foo = {
sendRequest: ...
The function value is only affected by the function expression, which doesn't contain a name.
... function() {
alert(bar.getUrl());
}
You need to include the name you want in the function expression itself [fiddle].
var foo = {
sendRequest: function sendRequest() {
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 doesn't have name which is true:
function() {
This is anonymous function.
You can name you method : sendRequest: function myMethodName() {
Although the function is stored under the object property foo.sendRequest
, and thus can be invoked via foo.sendRequest()
, that function itself doesn't actually have a name. That's why arguments.callee.caller.name
is empty.
Because the function that is calling the function being called is an anonymous function (and hence, has no 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();
本文标签: javascriptWhy is argumentscalleecallername undefinedStack Overflow
版权声明:本文标题:javascript - Why is arguments.callee.caller.name undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745538911a2662401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论