[英]Javascript calling a class function
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\">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; }
}
}
I expect it to alert "cometishian" but this.gDebugPannel.innerHTML
is null, any ideas? 我希望它提醒“cometishian”但是
this.gDebugPannel.innerHTML
是空的,有什么想法吗?
Upon further investigation this.gDebugPannel is undefined. 经过进一步调查,this.gDebugPannel未定义。
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: 这行this.gDebug(“ wo”)抛出:
Webpage error details 网页错误详情
User Agent: Mozilla/4.0 (compatible; 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 用户代理: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日星期四
Message: Object doesn't support this property or method
Line: 21
Char: 5
Code: 0
URI: http://server1/bb/ganttnew/gMain.js
You need to call the function on the this
instance, like this: 您需要在
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.