method in JavaScript object didnt work -


i write sourse, dont understand why if put this.isexist() function inside chat() function (the main function), didnt work.

but if put this.isexsist() function outside this.chat function, works well.

someone can explain me why this?


here source didnt work:

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

here source works fine:

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

you using "method" before it's being defined!

just grab (above line uses it) , should work fine.

hope helps

function chat(id, nic) {     // ....      if (this.isexist(id) == false) {     // @ stage, method not yet defined         this.arrchat['i']++;             // should put above line 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;     }      // .... } 

hope helps


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -