If I understand correctly, the problem is to call a bd.collection
method, via a string
, with the method name, which probably comes from another code location. Something like: call the specified method in example.method
, whatever it is.
Suppose the situation:
var bd = {
collection: {
find: function (keyword) {
console.log(keyword);
}
}
}
Where the method is specified in:
const example = {method: "find"};
One way is to access the object by index, through the method name:
bd.collection[example.method]("SOpt");
That generates exactly the same output as the direct call of the method:
bd.collection.find("SOpt");
Below, I've entered the complete code to be able to run.
var bd = {
collection: {
find: function (keyword) {
console.log(keyword);
}
}
}
const example = {method: "find"};
bd.collection[example.method]("SOpt");
bd.collection.find("SOpt");