码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript获取Function的名字

时间:2014-10-24 23:27:27      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   java   

如何获取JavaScript中Function的名字是一个很基本的问题。可以通过将function转化成字符串,用substring取它的名字,或者使用ECMAScript 6中的Function.name的支持。此属性只有在IE下有可能不兼容。详见MDN.

关于从字符串中获得其名字的方法如下 (转自stackoverflow):

function functionName(fun) {
  var ret = fun.toString();
  ret = ret.substr(‘function ‘.length);
  ret = ret.substr(0, ret.indexOf(‘(‘));
  return ret;
}

如其在stackoverflow中提到的,Function.caller是非标准的用法,arguments.callee不在strick mode下支持。

 

JavaScript获取Function的名字

标签:style   blog   http   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/rixin/p/4049473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!