myChart = new ganttChart("chart1");
function ganttChart(gContainerID) {
this.variable1 = "lol";
this.variable2 = "hai dere";
this.variable3 = "cometishian";
....
this.gContainer = document.getElementById(gContainerID);
this.gContainer.innerHTML += "<div id=\"gBar" + i +
"\" class=\"gBar\" onmousedown=\"gInitBarDrag(this)\">Hello</div>";
....
}
我將如何讓甘特圖類中定義的函數gInitBarDrag()?我不想要一個外部獨立函數,因爲它需要引用對象內部的東西。
因此,例如,該函數將能夠引用在ganttChart對象的特定實例(您可以有多個圖表對象)中定義的變量1/2/3。
希望有道理! EG:
function ganttChart(gContainerID) {
this.variable1 = "lol";
this.variable2 = "hai dere";
this.variable3 = "cometishian";
....
this.gContainer.innerHTML += "<div id=\"gBar" + i +
"\" class=\"gBar\" onmousedown=\"gInitBarDrag(this)\">Hello</div>";
....
gInitBarDrag = function(thisGanttObject)
{
alert(thisGanttObject.variable2);
// This line wont work because it doesn't know what ganttChart to reference!
}
}
@Tom這個'價值指向什麼? – 2010-12-03 17:12:27
甘特圖對象。 – 2010-12-03 17:14:25
@Tom「granttChart class」是什麼意思? granttChart是一個函數,無論如何,JavaScript中都沒有類。 – 2010-12-03 17:15:14