Javascript, вызывающий функцию класса - PullRequest
0 голосов
/ 25 ноября 2010

Я пытаюсь вызвать функцию класса:

// Gantt chart object
function ganttChart(gContainerID) {

    this.isDebugMode = true;                                    // Is this chart in debug mode
    this.gContainer = document.getElementById(gContainerID);    // The container the chart is built inside
    this.gDebugPannel;                                          // Debug pannel

    // Create debug pannel
    if (this.isDebugMode) {
        this.gContainer.innerHTML += "<div id=\"gDebug" + gContainerID + "\" class=\"gDebug\">cometishian</div>";
        this.gDebugPannel = document.getElementById("gDebug" + gContainerID);
    }

    gDebug("lmfao!");

// Updates debug information
function gDebug(debugMessage) {
    alert(this.gDebugPannel.innerHTML);
    if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
}
}

Я ожидаю, что это предупредит "комет", но this.gDebugPannel.innerHTML - ноль, есть идеи?

При дальнейшем исследовании this.gDebugPannel не определен.

Обновление:

// Gantt chart object
function ganttChart(gContainerID) {

    this.isDebugMode = true;                                    // Is this chart in debug mode
    this.gContainer = document.getElementById(gContainerID);    // The container the chart is built inside
    this.gDebugPannel;                                          // Debug pannel
    this.gPosX;
    this.gPosY;

    // Create debug pannel
    if (this.isDebugMode) {
        this.gContainer.innerHTML += "<div id=\"gDebug" + gContainerID + "\" class=\"gDebug\">5,5 | 5.1</div>";
        this.gDebugPannel = document.getElementById("gDebug" + gContainerID);
    }

    // Updates debug information
    ganttChart.gDebug = function(debugMessage) {
        if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
    }

    this.gDebug("wo");

}

строка this.gDebug ("wo") выдает:

Сведения об ошибке веб-страницы

Пользовательский агент: Mozilla / 4.0 (совместимый; MSIE 8.0; Windows NT 5.1; Trident / 4.0; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.4; OfficeLivePatch.1.3) Отметка времени: четверг, 25 ноября 2010 г., 12:57:51 UTC

Message: Object doesn't support this property or method
Line: 21
Char: 5
Code: 0
URI: http://server1/bb/ganttnew/gMain.js

Ответы [ 2 ]

1 голос
/ 25 ноября 2010

Вы также можете сделать это как

this.gDebug= function(debugMessage) {
    alert(this.gDebugPannel.innerHTML);
    if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
}
1 голос
/ 25 ноября 2010

Вам необходимо вызвать функцию в экземпляре this, например:

gDebug.call(this, "Hi!");

Правильный способ сделать это - поместить функцию в прототип класса: (Это должно быть сделано послеобъявление функции конструктора)

ganttChart.prototype.gDebug = function(debugMessage) {
    alert(this.gDebugPannel.innerHTML);
    if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
}

this.gDebug("Hi!");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...