alexandernst
alexandernst

Reputation: 15099

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()).

My question is: Is it possible, and if it is, how, to get the source code of myFunc programatically? (This is inside Node)

Upvotes: 1

Views: 557

Answers (1)

Jamie Dixon
Jamie Dixon

Reputation: 54011

You can obtain the function as a string using the toString method that sits on Function.prototype.

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

Upvotes: 3

Related Questions