i've created an object that contains the names of several objects and the names of functions associated with creating new objects, in this case, charts.
var oCharts = [{
nchart: "chart1",
fchart: "makeChart1"
},
{
nchart: "chart2",
fchart: "makeChart2"
},
{
nchart: "chart3",
fchart: "makeChart3"
}
]
"nchart" is the variable representing the chart to be destroyed. "fchart" is the variable representing the function to create a new chart.
If i do it manually, like this, it works
if(typeof chart1 != undefined ){
chart1.destroy();
makeChart1();
}
If i iterate over oCharts, it doesn't work. I get "Object doesn't support property or method 'destroy'" and, of course, it never gets far enough to run makeChart(). (i'm not even sure I'm calling that correctly.)
$(oCharts).each(function(i,v){
var myChart = [this.nchart];
var makeChart = this.fchart;
if(typeof myChart != undefined ){
myChart.destroy();
makeChart();
}
});
the functions to make charts are simply...
function makeChart1(){
// do stuff
}
function makeChart2(){
// do other stuff
}
function makeChart3(){
// do different stuff
}
Can someone please tell me what I'm doing wrong? It would be very much appreciated!
evalto invoke the functions by the string instead of its reference which is a bad idea. You are running into the error as you are using()on astringtypeof myChartis astringand only functions have the properties to invoke ittypeof myChart != undefinedalways will be true becausetypeof variablealways returnstringtype and you should compare it with stringified value ofundefinedi.e.typeof myChart !== 'undefined'Also, before this expression you create a variable ` var myChart = [this.nchart]; `typeof myChartalways will return 'object'. To prevent you need just assignthis.ncharttomyChart