最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript calling a class function - Stack Overflow

programmeradmin1浏览0评论

I'm trying to call a class function:

// 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\">etishian</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; }
}
}

I expect it to alert "etishian" but this.gDebugPannel.innerHTML is null, any ideas?

Upon further investigation this.gDebugPannel is undefined.

Update:

// 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");

}

the line this.gDebug("wo") throws:

Webpage error details

User Agent: Mozilla/4.0 (patible; 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) Timestamp: Thu, 25 Nov 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

I'm trying to call a class function:

// 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\">etishian</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; }
}
}

I expect it to alert "etishian" but this.gDebugPannel.innerHTML is null, any ideas?

Upon further investigation this.gDebugPannel is undefined.

Update:

// 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");

}

the line this.gDebug("wo") throws:

Webpage error details

User Agent: Mozilla/4.0 (patible; 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) Timestamp: Thu, 25 Nov 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
Share Improve this question edited Nov 25, 2010 at 12:57 Tom Gullen asked Nov 25, 2010 at 12:45 Tom GullenTom Gullen 61.7k88 gold badges291 silver badges469 bronze badges 1
  • The statement this.gDebugPannel; does nothing. – SLaks Commented Nov 25, 2010 at 12:51
Add a ment  | 

2 Answers 2

Reset to default 1

You need to call the function on the this instance, like this:

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

The correct way to do this is to put the function in the class prototype: (This should be done after declaring the constructor function)

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

this.gDebug("Hi!");

you can also do it as

this.gDebug= function(debugMessage) {
    alert(this.gDebugPannel.innerHTML);
    if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
}
发布评论

评论列表(0)

  1. 暂无评论