网上很多文章都转载了一段话:对于函数来说,caller 属性只有在函数执行时才有定义。
到底是不是呢?可以用下面这段代码测试一下(这段例子也是转载文章中使用的):
function callerDemo() { if (callerDemo.caller) { var a= callerDemo.caller.toString(); alert(a); } else { alert(callerDemo.caller); } } function handleCaller() { callerDemo(); } alert("callerDemo.caller:"+callerDemo.caller); //null alert("callerDemo.callee:"+callerDemo.callee); //undefined alert("callerDemo.undefinedProperties:"+callerDemo.undefinedProperties); //undefined
可以看出任何未定义的属性打印出来的值都是undefined
而caller属性打印出来的却是null,说明caller属性在执行前就已经存在,只不过为null而已
本文通过一个具体的示例探讨了JavaScript中函数的caller属性,并验证了该属性在函数被调用之前是否存在及其具体值。
3326

被折叠的 条评论
为什么被折叠?



