【问题标题】:Javascript calling a class functionJavascript调用类函数
【发布时间】:2011-05-15 16:49:13
【问题描述】:

我正在尝试调用一个类函数:

// 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) 时间戳:2010 年 11 月 25 日星期四 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

【问题讨论】:

  • 声明 this.gDebugPannel; 什么都不做。

标签: javascript class function scope


【解决方案1】:

您需要在this 实例上调用该函数,如下所示:

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

正确的做法是将函数放在类原型中:(这应该在声明构造函数后进行)

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

this.gDebug("Hi!");

【讨论】:

  • 太棒了,为什么当函数被定义为接受 1 时,当传递两个参数时这会起作用?
  • @Tom:call 方法采用上下文 (this),后跟要传递给函数的参数。 developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
  • 谢谢!我已经按照你说的做了原型(我想正确地做到这一点)但是我收到一个错误,如果它对你来说不是太大的问题,你能看看更新的代码
  • 啊,不是 ganttChart.gDebug 应该是 this.gDebug 吧?
【解决方案2】:

你也可以这样做

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多