方法在JavaScript对象中不起作用
method in JavaScript object didnt work
我写这个源代码,但我不明白为什么如果我把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;
}
// ....
}
希望有所帮助
相关文章:
- 面向对象的Javascript代码在IE7中不起作用
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象文字方法上的Javascript绑定不起作用
- 带有普通对象的Ember.js查找不起作用
- 在SVG中创建带有外来对象的文本区域的Javascript不起作用
- 函数中的 angularJS 访问对象属性不起作用
- JavaScript参数对象不起作用
- 当图像和链接是 json 对象属性的值时,它们不起作用
- JavaScript Audio 对象在 Android API 19 中不起作用
- 为什么从 Angular 服务中的对象中删除此元素不起作用
- jQuery父对象(“tr”)淡出不起作用
- 将JSON对象转换为敲除observable在IE8中不起作用,但在所有其他浏览器中都起作用
- Javascript敲除绑定嵌套对象不起作用
- 映射 jQuery 对象的数组不起作用
- 对象原型在Canvas Game中不起作用
- AngularJS-对象分配不起作用
- javascript对象检查在IE11中不起作用
- PHP将JSON对象返回到Javascript[AAJAX CALL]不起作用
- 将JAX-RS与ajax和javascript's blob对象,不'不起作用
- 函数中的对象访问;不起作用