Javascript dynamically invoke object method from string Javascript dynamically invoke object method from string javascript javascript

Javascript dynamically invoke object method from string


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

foo[method]();


Properties of objects can be accessed through the array notation:

var method = "smile";foo[method](); // will execute the method "smile"


When we call a function inside an object, we need provide the name of the function as a String.

var obj = {talk: function(){ console.log('Hi') }};obj['talk'](); //prints "Hi"obj[talk]()// Does not work