javascript Javascript调用类函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4277205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript calling a class function
提问by Tom Gullen
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.innerHTMLis null, any ideas?
我希望它提醒“彗星”但它this.gDebugPannel.innerHTML是空的,有什么想法吗?
Upon further investigation this.gDebugPannel is undefined.
经过进一步调查,this.gDebugPanel 未定义。
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;.1.11CLR 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
回答by SLaks
You need to call the function on the thisinstance, 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!");
回答by Naren Sisodiya
you can also do it as
你也可以这样做
this.gDebug= function(debugMessage) {
alert(this.gDebugPannel.innerHTML);
if (this.isDebugMode) { this.gDebugPannel.innerHTML = debugMessage; }
}

