javascript获取功能体 [英] javascript get function body

查看:102
本文介绍了javascript获取功能体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,例如

var test = function () {alert(1);}

如何获得此功能的正文?

How can I get the body of this function?

I假设唯一的方法是解析test.toString()方法的结果,但还有其他方法吗?如果解析是唯一的方法,那么正则表达式将是什么? (极少需要正则表达式的帮助,因为我不熟悉它们)

I assume that the only way is to parse the result of test.toString() method, but is there any other way? If parsing is the only way, what will be the regex to get to body? (help with the regex is extremly needed, because I am not familiar with them)

推荐答案

IF( !!!) 你可以得到 toString(),然后你可以从第一个 indexOf中获取子串({) lastIndexOf(})。所以,像这样的工作(在ideone.com上看到):

IF(!!!) you can get the toString(), then you can simply take the substring from the first indexOf("{") to the lastIndexOf("}"). So, something like this "works" (as seen on ideone.com):

var test = function () {alert(1);}

var entire = test.toString(); // this part may fail!
var body = entire.substring(entire.indexOf("{") + 1, entire.lastIndexOf("}"));

print(body); // "alert(1);"

这篇关于javascript获取功能体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆