darkandcold
darkandcold

Reputation: 105

how to get a value which points to a function

i have a value like that

var myvalue = myfunction(1,2);

what I need is that GETTING myfunction(a,b) as a string..

I mean not "myfunction's value"

hmmm, let me explain,

myfunction(1,2) returns 1+2=3

if I type

alert(myvalue)

it returns 3 but I need myfunction(a,b) AS IT'S TYPED when I use alert. NOT IT'S VALUE

think it like

var myvalue='myfunction(a,b)'

now, if i use Alert, it gives me myfunction(a,b)

how can I do that?

Upvotes: 3

Views: 107

Answers (2)

Justin Johnson
Justin Johnson

Reputation: 31300

If you want to get the string value of a function, you can use the builtin toString method

var f1 = function(a, b) {
    return a + b;
};

function f2(a, b) {
    return a + b;
};

console.log(f1.toString());
console.log(f2.toString());

yields

function (a, b) {
    return a + b;
}
function f2(a, b) {
    return a + b;
}

But why do you want to do this?

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

var myvalue = function()
{
  myfunction(1,2);
};

myvalue is a anonymous function that calls myfunction with the specified parameters. You can call it, and if you print it for debugging, it will look something like:

function () { 
   myfunction(1, 2); 
}

Upvotes: 2

Related Questions