invoking a method when I have it's name as a string

A

Andy Fish

Hi,

I'm trying to figure out how to invoke a method on an object given the
object and the method name. For example, here's a simple object with a
method:

function MyObj (pName, pDesc) {
this.name = pName;
this.desc = pDesc;
MyObj.prototype.foo = function() {
return this.name + " - " + this.desc;
}
}

and here is some sample code to manipulate it.

var o1 = new MyObj("nn", "dd");
var s1 = o1.foo();

The assignment to s1 invokes the foo method and gets back the string "nn -
dd" as expected. now I try to find the foo method by it's name:

var funcName = "foo";
var theFunc = o1[funcName];
var s2 = theFunc();

the assignment to s2 invokes the function OK, but not in the context of the
object. can someone tell me how to invoke a function as a method?

Andy
 
L

Lasse Reichstein Nielsen

Andy Fish said:
The assignment to s1 invokes the foo method and gets back the string "nn -
dd" as expected. now I try to find the foo method by it's name:

var funcName = "foo";
var theFunc = o1[funcName];
var s2 = theFunc();

the assignment to s2 invokes the function OK, but not in the context of the
object. can someone tell me how to invoke a function as a method?

theFunc.call(o1)

There are two methods of function objects that allow them to pretend to
be methods: "call" and "apply". The only difference is in how they transfer
the arguments to the function. "Call" takes the arguments one at a time,
and "apply" takes them as one array (and it is a type error if the second
argument of "apply" isn't an array).

function myFunc(arg1,arg2,arg3) {...}

myFunc.call(myObj,arg1,arg2,arg3);
myFunc.apply(myObj,[arg1,arg2,arg3]);

/L
 
V

Vjekoslav Begovic

Andy Fish said:
Hi,

I'm trying to figure out how to invoke a method on an object given the
object and the method name. For example, here's a simple object with a
method:

function MyObj (pName, pDesc) {
this.name = pName;
this.desc = pDesc;
MyObj.prototype.foo = function() {
return this.name + " - " + this.desc;
}
}

and here is some sample code to manipulate it.

var o1 = new MyObj("nn", "dd");
var s1 = o1.foo();

The assignment to s1 invokes the foo method and gets back the string "nn -
dd" as expected. now I try to find the foo method by it's name:

var funcName = "foo";
var theFunc = o1[funcName];
var s2 = theFunc();

the assignment to s2 invokes the function OK, but not in the context of the
object. can someone tell me how to invoke a function as a method?

var funcName = "foo";
var s2 = o1[funcName]();
 
M

Martin Honnen

Andy said:
Hi,

I'm trying to figure out how to invoke a method on an object given the
object and the method name. For example, here's a simple object with a
method:

function MyObj (pName, pDesc) {
this.name = pName;
this.desc = pDesc;
MyObj.prototype.foo = function() {
return this.name + " - " + this.desc;
}
}

and here is some sample code to manipulate it.

var o1 = new MyObj("nn", "dd");
var s1 = o1.foo();

The assignment to s1 invokes the foo method and gets back the string "nn -
dd" as expected. now I try to find the foo method by it's name:

var funcName = "foo";
var theFunc = o1[funcName];
var s2 = theFunc();

the assignment to s2 invokes the function OK, but not in the context of the
object. can someone tell me how to invoke a function as a method?

Once you assign the method to a variable that function is unbound thus
don't do that, simply call the method on the object
var s2 = o1[funcName]()
 
A

Andy Fish

Hmm, now I'm confused again. how come this works:

var s2 =(o1[funcName])();

works but this doesn't:

var theFunc = o1[funcName];
var s2 = theFunc();

you'll notice I even put o1[funcName] in brackets for the first example,
which surely must force it to be evaluated as an expression which evaluates
to a value which should be able to be stored in a variable. I even tried
this slightly strange construct:

var s2 =(theFunc = o1[funcName])();

and that doesn't work either.

Vjekoslav Begovic said:
Andy Fish said:
Hi,

I'm trying to figure out how to invoke a method on an object given the
object and the method name. For example, here's a simple object with a
method:

function MyObj (pName, pDesc) {
this.name = pName;
this.desc = pDesc;
MyObj.prototype.foo = function() {
return this.name + " - " + this.desc;
}
}

and here is some sample code to manipulate it.

var o1 = new MyObj("nn", "dd");
var s1 = o1.foo();

The assignment to s1 invokes the foo method and gets back the string "nn -
dd" as expected. now I try to find the foo method by it's name:

var funcName = "foo";
var theFunc = o1[funcName];
var s2 = theFunc();

the assignment to s2 invokes the function OK, but not in the context of the
object. can someone tell me how to invoke a function as a method?

var funcName = "foo";
var s2 = o1[funcName]();
 
A

Andy Fish

Lasse Reichstein Nielsen said:
Andy Fish said:
Hmm, now I'm confused again. how come this works:

var s2 =(o1[funcName])();

works but this doesn't:

var theFunc = o1[funcName];
var s2 = theFunc();
you'll notice I even put o1[funcName] in brackets for the first example,
which surely must force it to be evaluated as an expression which evaluates
to a value which should be able to be stored in a variable.

Not really. Brackets are only grouping, not meaningful
<snip>

OK, I get it now.

I thought that by putting brackets round it, I would force it to be a
"normal" expression (i.e. one with only one value). It all makes sense when
you add in the concept of reference expressions.

I prostrate myself at the master of javascript, sorry ECMAScript.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top