我写这个论坛,但我不明白为什么如果我把this.isExist()
函数放在<{1}}函数(主函数)里面,它没有用。
但如果我将chat()
函数放在 this.isExsist()
函数之外,那么它也可以正常工作。
以下是无法使用的来源:
this.chat
这里的工作正常:
<a href="javascript: chat(1,111);"> new chat 1</a><br />
<a href="javascript: chat(2,222);"> new chat 2</a><br />
<a href="javascript: chat(3,333);"> new chat 3</a><br />
<a href="javascript: chat(4,444);"> new chat 4</a><br />
<a href="javascript: chat(5,555);"> new chat 5</a><br />
<div id='chatBar.1' style='float:left; position:fixed; bottom:0px; left:10px;'></div>
<script>
function chat(id, nic) {
this.max = 3;
if (typeof(this.arrChat) == "undefined") {
this.arrChat = new Array();
this.arrChat['i'] = 0;
this.arrChat['n'] = 0;
}
if (this.isExist(id) == false) { // here i use the 'this.isExist()' function
this.arrChat['i']++;
this.arrChat['n']++;
this.arrChat[this.arrChat['i']] = [id,nic];
design = "<div id='chatBar."+(this.arrChat['n']+1)+"' style='float:right;'>nnn</div><div id='chatInfo."+this.arrChat['i']+"' style='float:right;'>info "+this.arrChat['i']+" - "+this.arrChat[this.arrChat['i']].toString()+"</div>";
document.getElementById('chatBar.'+this.arrChat['n']).innerHTML = design;
}
this.isExist = function (id) { // this function inside the 'chat()' function
if (this.arrChat['n'] == 0) { return false; }
for (i=1; i<=this.arrChat['i']; i++) {
if (id == this.arrChat[i][0]) {
return true;
}
}
return false;
}
}
</script>
答案 0 :(得分:1)
您正在使用"method"
之前定义它!
抓住它(在使用它的线上方)并且应该可以正常工作。
希望有所帮助
function chat(id, nic) {
// ....
if (this.isExist(id) == false) { // At this stage, your method is not yet defined
this.arrChat['i']++; // It should be put above this line for example
this.arrChat['n']++;
this.arrChat[this.arrChat['i']] = [id,nic];
design = "<div id='chatBar." + (this.arrChat['n'] + 1)
+ "' style='float:right;'> nnn </div> <div id='chatInfo."
+ this.arrChat['i'] + "' style='float:right;'> info "
+ this.arrChat['i'] + " - "
+ this.arrChat[this.arrChat['i']].toString()
+ "</div>";
document.getElementById('chatBar.' + this.arrChat['n']).innerHTML = design;
}
// ....
}
希望有所帮助