简体   繁体   English

获取函数原型的源代码

[英]Get source code of a functions prototype

Let's say I do the following: 假设我做了以下事情:

String.prototype.myFunc = function(){ /* code here */ }

I have just created a new method, myFunc , that will be available for each string ( "This is a test".myFunc() ). 我刚刚创建了一个新方法myFunc ,它可用于每个字符串( "This is a test".myFunc() )。

My question is: Is it possible, and if it is, how, to get the source code of myFunc programatically? 我的问题是:以编程方式获取myFunc的源代码是否可能,如果是,如何? (This is inside Node) (这是在Node内部)

您可以使用位于Function.prototype上的toString方法将函数作为字符串获取。

var funcSource = String.prototype.myFunc.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM