JS查看自身函数和父函数的方式

来源:互联网 发布:台服dnf登陆器源码 编辑:程序博客网 时间:2024/05/23 10:51

如何在脚本运行的时候查看他的父函数——callee和caller两个属性(不是方法)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><script src="lib/jquery-1.6.4.min.js" type="text/javascript"></script><script type="text/javascript">$(document).ready(function(){parent();});function parent(){caller1();}function caller1(){//functionName.caller 返回一个对函数的引用(即调用本函数的父函数),该函数调用了当前函数。//如果函数是由 JScript 程序的顶层调用的,那么 caller 包含的就是 null 。alert(caller1.caller);}function callee1(){//callee是arguments的属性,返回正被执行的 Function 对象,也就是所指定的 Function 对象的正文。alert(arguments.callee);}function callee2(n){//使用递归的方式调用自身的函数if(isNaN(parseInt(n))){alert("传入的参数应该是数字!");return false;}alert(n);if(n<=1){return 1;}else{//callee(n-1)实际上调用的函数为callee2(n-1)return arguments.callee(n-1);}}</script><title>无标题文档</title></head><body><button onclick="parent();">caller</button><br><button onclick="callee1();">callee1</button><br><button onclick="callee2(4);">callee2</button></body></html>

 

原创粉丝点击