How to get a functions's body as string?

后端 未结 4 1423
不思量自难忘°
不思量自难忘° 2020-12-06 17:07

I want to know how to convert a function\'s body into a string?

function A(){
  alert(1);
}

output = eval(A).toString() // this will come with  function A()         


        
4条回答
  •  感情败类
    2020-12-06 17:54

    If you're going to do something ugly, do it with regex:

    A.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1];
    

提交回复
热议问题