zoukankan      html  css  js  c++  java
  • 获取函数名

    如果当前函数是有名函数,则返回其名字,如果是匿名函数则返回被赋值的函数变量名,如果是闭包中匿名函数则返回“anonymous”。

         var getFnName = function(callee){
          var _callee = callee.toString().replace(/[\s\?]*/g,""),
          comb = _callee.length >= 50 ? 50 :_callee.length;
          _callee = _callee.substring(0,comb);
          var name = _callee.match(/^function([^\(]+?)\(/);
          if(name && name[1]){
            return name[1];
          }
          var caller = callee.caller,
          _caller = caller.toString().replace(/[\s\?]*/g,"");
          var last = _caller.indexOf(_callee),
          str = _caller.substring(last-30,last);
          name = str.match(/var([^\=]+?)\=/);
          if(name && name[1]){
            return name[1];
          }
          return "anonymous"
        };
    

    使用:在要调查的函数内部执行此函数,传入一个参数,为arguments.callee。

        function  ee(){
          //+++++++++++++++++++++++++++++++++
          var fnname =getFnName(arguments.callee)
          //+++++++++++++++++++++++++++++++++
          alert(fnname)
        };
        ee();
    

  • 相关阅读:
    我们为什么要使用Spring Cloud?简介
    Spring注解@Component、@Repository、@Service、@Controller区别
    Spring boot 各种入门及问题
    spring boot与spring mvc的区别是什么?
    Android http Request / Response ContentType
    Android Studio添加aar
    Android 自定义线程池的实战
    Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
    EditText 显示明文和密码
    Android log 管理工具
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/1597523.html
Copyright © 2011-2022 走看看