为什么我的这个JavaScript对象不能执行? 帮我看下,顺便评论下我的代码是不是规范的

用户昵称不能为空 2010-01-04 02:57:47


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>

<body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21

var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
_start && this.start = _start;
_len && this.len = _len;
setTimeout('display(this.obj)',this.start*1000);
setTimeout('display(this.obj)',(this.start+this.len)*1000);
}
};

function display(obj){
var s = obj.style;
s.display = s.display ? '':'none';
}

//使用
tip.tip(document.getElementById('tip'),2,5);

</script>
</body>
</html>



总是有错误,因为时间紧迫,所以换了另外一种方法了。
大家帮我看下为什么会错误,这个函数的功能就是当页面载入多少秒后这个div显示,多少秒后div自动隐藏。
顺便评论我的代码是不是规范的JavaScript。
...全文
212 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
curacfyh 2010-01-05
  • 打赏
  • 举报
回复
16楼老虎兄弟的书我收下了,谢谢~
csdnfan 2010-01-05
  • 打赏
  • 举报
回复
我终于调试好了,呵呵,关键是那个参数没有传进去,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>

<body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21

var tip={
obj:0, //DIV 或者SPAN 对象
start:2, //开始的秒数
len:5, //显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
//这里的两个&&不知道是做什么的
// _start && this.start = _start;
// _len && this.len = _len;
this.start = _start;
this.len = _len;
//这里的参数以前没有传进去要改了
setTimeout(display(this.obj),this.start*1000);
setTimeout(display(this.obj),(this.start+this.len)*1000);
}
};
//这个函数改了一下
function display(obj) {
return function() {
var s = obj.style;
s.display = s.display ? '' : 'none';
}
}

//使用
tip.tip(document.getElementById('tip'),2,5);

</script>
</body>
</html>

zhangshaolongjj 2010-01-05
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>

<body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21

var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
that = this;
this.obj = _obj;
this.start = _start || this.start;
this.len = _len || this.len;
setTimeout('display(that.obj)',this.start*1000);
setTimeout('display(that.obj)',(this.start+this.len)*1000);
}
};

function display(obj){

var s = obj.style;
s.display = s.display ? '':'none';
}

//使用
tip.tip(document.getElementById('tip'),2,5);

</script>
</body>
</html>
yixianggao 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 default7 的回复:]
代码哪里 不规范啊,麻烦大家说详细下啊。
[/Quote]
有时间读读《Clean Code》,中文版《代码整洁之道》,
不过建议尽量读 E 文版,csdn 下载里有免费 pdf 的!

程序员需要 Code Sence,lz 的代码中根本看不到,需要慢慢培养!
  • 打赏
  • 举报
回复
代码哪里 不规范啊,麻烦大家说详细下啊。
antony1029 2010-01-05
  • 打赏
  • 举报
回复
学习!
浴火_凤凰 2010-01-05
  • 打赏
  • 举报
回复
总得有错误描述吧
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 xinsiyu2008 的回复:]
我终于调试好了,呵呵,关键是那个参数没有传进去,代码如下:
HTML code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type" content="text/html; charset=gb2312"/><title></title></head><body><divid="tip" style="display:none">提示的内容!</div><scriptlanguage="javascript">//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象 start:2,//开始的秒数 len:5,//显示的秒数 tip:function(_obj,_start,_len){this.obj= _obj;//这里的两个&&不知道是做什么的// _start && this.start = _start;// _len && this.len = _len;this.start= _start;this.len= _len;//这里的参数以前没有传进去要改了 setTimeout(display(this.obj),this.start*1000);
setTimeout(display(this.obj),(this.start+this.len)*1000);
}
};//这个函数改了一下function display(obj) {returnfunction() {var s= obj.style;
s.display= s.display?'' :'none';
}
}//使用 tip.tip(document.getElementById('tip'),2,5);</script></body></html>
[/Quote]


那个&&是PHP的写法,原理js不支持这样。
hhfandzyq 2010-01-04
  • 打赏
  • 举报
回复
代码不规范啊!
jiewenxu 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cnfbx 的回复:]
引用 7 楼 jiewenxu 的回复:
引用 6 楼 kk3k2005 的回复:
setTimeout("tip.display()",this.start*1000);   这个肯定错了

  改成
       setTimeout(function(){tip.display()},this.start*1000);

  上下2个都是


明明是对的。。不要误导别人。tip是全局变量,有什么错?不信你可以运行一下。当然你的写法也正确。

setTimeout的第一个参数是对一个函数的引用,也就是参数是一个指针,如果用tip.display(),那么此时参数就是tip.display()的返回值,当然不是我们想要的.可以用匿名函数function(){tip.display()},如果想要传入参数的话,可以加一个壳,比如:
JScript code
setTimeout(fun1(arg1,arg2),num);function fun1(arg1,arg2){returnfunction(){//这里可以用参数了 }
}
[/Quote]

我的说法是针对他当前的用法——display方法没有参数,tip是全局变量。
你说的也对。这里使用匿名函数更多是因为传入的变量是函数的局部变量的情况
cnfbx 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jiewenxu 的回复:]
引用 6 楼 kk3k2005 的回复:
setTimeout("tip.display()",this.start*1000);   这个肯定错了

改成
     setTimeout(function(){tip.display()},this.start*1000);

上下2个都是




明明是对的。。不要误导别人。tip是全局变量,有什么错?不信你可以运行一下。当然你的写法也正确。
[/Quote]
setTimeout的第一个参数是对一个函数的引用,也就是参数是一个指针,如果用tip.display(),那么此时参数就是tip.display()的返回值,当然不是我们想要的.可以用匿名函数function(){tip.display()},如果想要传入参数的话,可以加一个壳,比如:

setTimeout(fun1(arg1,arg2),num);
function fun1(arg1,arg2){
return function(){
//这里可以用参数了
}
}
jiewenxu 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 default7 的回复:]
引用 4 楼 yixianggao 的回复:
可以运行了,但无任何代码规范性可言!

L@_@K
HTML code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type" content="text/html; charset=gb2312"/> <title> </title> </head> <body> <divid="tip" style="display:none">提示的内容! </div> <scriptlanguage="javascript">//
// 2010-1-4 14:48:21var tip={
    obj:0,//DIV 或者SPAN 对象    start:2,//开始的秒数    len:5,//显示的秒数    tip:function(_obj,_start,_len){this.obj= _obj;this.start= _start;this.len= _len;
        setTimeout("tip.display()",this.start*1000);
        setTimeout("tip.display()",(this.start+this.len)*1000);
    },
    display:function(){this.obj.style.display=this.obj.style.display?'':'none';
    }
};//使用tip.tip(document.getElementById('tip'),2,5); </script> </body> </html>


我就是想,有可能在使用tip.tip()的时候,可能是这样的
tip.tip(document.getElementById('tip'));
,那么就默认用原来的那个时间。
你的这个代码不能这样。
另外一个函数假如他有三个参数,但是我使用的时候只写了两个,而JavaScript不能像PHP那样直接在函数名称的参数那里写初始值,即function PHPfunc($a='b',$c='d'){}

[/Quote]

JS里面定义参数默认值一般是这样的


function test(a,b,c) {
a = a||'defaultValue Of a';
b = b||'defaultValue Of b';
c = c||'defaultValue Of c';

document.write('a:'+a+'<br/>b:'+b+'<br/>c:'+c+'<br/><br/>');
}

test();
test('new Value Of a');
test('new Value Of a','new Value Of b');



再扯代码规范性:你面向对象的思想应该再提高提高。一个对象的方法和属性应该集中起来。你把display方法拆了出来。这个是不对的。

jiewenxu 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kk3k2005 的回复:]
setTimeout("tip.display()",this.start*1000);  这个肯定错了

改成
    setTimeout(function(){tip.display()},this.start*1000);

上下2个都是


[/Quote]

明明是对的。。不要误导别人。tip是全局变量,有什么错?不信你可以运行一下。当然你的写法也正确。
KK3K2005 2010-01-04
  • 打赏
  • 举报
回复
setTimeout("tip.display()",this.start*1000); 这个肯定错了

改成
setTimeout(function(){tip.display()},this.start*1000);

上下2个都是

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yixianggao 的回复:]
可以运行了,但无任何代码规范性可言!

L@_@K
HTML code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type" content="text/html; charset=gb2312"/><title></title></head><body><divid="tip" style="display:none">提示的内容!</div><scriptlanguage="javascript">//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象 start:2,//开始的秒数 len:5,//显示的秒数 tip:function(_obj,_start,_len){this.obj= _obj;this.start= _start;this.len= _len;
setTimeout("tip.display()",this.start*1000);
setTimeout("tip.display()",(this.start+this.len)*1000);
},
display:function(){this.obj.style.display=this.obj.style.display?'':'none';
}
};//使用tip.tip(document.getElementById('tip'),2,5);</script></body></html>
[/Quote]

我就是想,有可能在使用tip.tip()的时候,可能是这样的
tip.tip(document.getElementById('tip'));
,那么就默认用原来的那个时间。
你的这个代码不能这样。
另外一个函数假如他有三个参数,但是我使用的时候只写了两个,而JavaScript不能像PHP那样直接在函数名称的参数那里写初始值,即function PHPfunc($a='b',$c='d'){}
yixianggao 2010-01-04
  • 打赏
  • 举报
回复
可以运行了,但无任何代码规范性可言!

L@_@K
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>

<body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21

var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
this.start = _start;
this.len = _len;
setTimeout("tip.display()",this.start*1000);
setTimeout("tip.display()",(this.start+this.len)*1000);
},
display: function(){
this.obj.style.display = this.obj.style.display ? '':'none';
}
};

//使用
tip.tip(document.getElementById('tip'),2,5);

</script>
</body>
</html>
itian 2010-01-04
  • 打赏
  • 举报
回复
_start && this.start = _start;
_len && this.len = _len;

--》

_start && (this.start = _start);
_len && (this.len = _len);
itian 2010-01-04
  • 打赏
  • 举报
回复
setTimeout()中的函数是不能带参数的,需要用另外的变通方法
xmliy 2010-01-04
  • 打赏
  • 举报
回复
试试:

var me = this;
setTimeout(function() {display(me.obj)},this.start*1000);
setTimeout(function() {display(me.obj)},(this.start+this.len)*1000);

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧