Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  95] [ 6]  / answers: 1 / hits: 78754  / 12 Years ago, sat, march 24, 2012, 12:00:00

Can I dynamically call an object method having the method name as a string? I would imagine it like this:



var FooClass = function() {
this.smile = function() {};
}

var method = smile;
var foo = new FooClass();

// I want to run smile on the foo instance.
foo.{mysterious code}(); // being executed as foo.smile();

More From » oop

 Answers
45

if the name of the property is stored in a variable, use []



foo[method]();

[#86623] Friday, March 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;