请帮我解决以下问题。
var test = new Object();
test.testInner = new Object();
test.testInner.main = function ()
{
Hello();
}
function Hello()
{
/**** Question: currently I am getting blank string with below code,
**** Is there any way to get function name as "test.testInner.main" over here? */
console.log(arguments.callee.caller.name);
}
test.testInner.main();
答案 0 :(得分:1)
test.testInner.main
引用了anonymous
(无名称)功能。
您可以通过为其指定名称来获取名称。修改后的代码:
var test = new Object();
test.testInner = new Object();
test.testInner.main = function main()
{
Hello();
}
function Hello()
{
/**** Question: currently I am getting blank string with below code,
**** Is there any way to get function name as "test.testInner.main" over here? */
console.log(arguments.callee.caller.name);
}
test.testInner.main();
答案 1 :(得分:0)
您可以在Javascript中设置函数的上下文。
function hello() {
console.log(this);
}
some.other.object = function() {
hello.call(this, arguments, to, hello);
}
这将是hello()中的some.other.object。
在您的示例中,调用者是main,并且它没有name属性,因为它是匿名的。就像这里:Why is arguments.callee.caller.name undefined?
此外,参数已弃用,因此不应使用。