如何在JavaScript中获取函数体文本? [英] How to get function body text in JavaScript?

查看:150
本文介绍了如何在JavaScript中获取函数体文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数derp(){a(); B(); C(); } 

derp.toString()将会返回function derp(){a(); b(); c();},但我只需要函数的主体,所以a(); b(); c();,因为我可以评估表达式。是否有可能以跨浏览器的方式执行此操作?

解决方案

var entire = derp.toString();
var body = whole.slice(entire.indexOf({)+ 1,entire.lastIndexOf(}));

console.log(body); //a(); b(); c();

请使用搜索,这是这个问题


function derp() { a(); b(); c(); }

derp.toString() will return "function derp() { a(); b(); c(); }", but I only need the body of the function, so "a(); b(); c();", because I can then evaluate the expression. Is it possible to do this in a cross-browser way?

解决方案

var entire = derp.toString(); 
var body = entire.slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));

console.log(body); // "a(); b(); c();"

Please use the search, this is duplicate of this question

这篇关于如何在JavaScript中获取函数体文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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