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!

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

  1. 368805 764055Aw, this became an extremely good post. In thought I would like to set up writing like that additionally – taking time and actual effort to generate a terrific article but what / issues I say I procrastinate alot by means of no means appear to get something completed. 450416

    Reply