方法在JavaScript对象中不起作用

method in JavaScript object didnt work

本文关键字:不起作用 对象 JavaScript 方法      更新时间:2023-09-26

我写这个源代码,但我不明白为什么如果我把this.isExist()函数 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>

下面是正常工作的源代码:

<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 outside the 'chat()' function
    for (i=1; i<=this.arrChat['i']; i++) {
        if (id == this.arrChat[i][0]) {
            return true;
        }
    }
    return false;
}
</script>

您正在使用"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;
    }
    // ....
}

希望有所帮助