Putting a variable into name of function javascrip

2019-08-03 08:02发布

问题:

I want to do the following:

var table = new google.visualization.XXXXX(document.getElementById(divId));

where the X's represent a variable that is set previously.

Just so you know, I'm using Google visualizations here, but I don't think that is necessarily relevant to this question.

回答1:

Use square bracket notation.

var XXXXX = "someString";
var table = new google.visualization[XXXXX](document.getElementById(divId));


回答2:

Pretty simple:

var table = new google.visualization[XXXXX](document.getElementById(divId));

Should do the trick.