Javascript: call object method using string only

This code doesn’t work.

var Modal = {
    init: function() {
        console.log("test");
    }
}
var objMethod = "Modal.init";
window[objMethod]();

I saw some answers that it can be called using this but I want to know how it can be called without using the object.

Modal["init"]();

Thank you!

18 thoughts on “Javascript: call object method using string only”

Leave a Comment