下面这段代码是不能运行的,但大家应该能很容易的猜出我想干什么,请问我如何才能做到呢?
关于javascript对象模型我一直都不是太明白,不知道这样的功能在javascript中应该如何描述,望高手不吝指教!
如果这个问题确实涉及到很高深的理论,我愿意加分。function foo( c )
{
this.cnt = c;
this.start = function()
{
setInterval( DoSomething, 1000 );
}
this.DoSomething = function()
{
this.cnt++; // just do something
}
}var f1 = new foo( 1 );
f1.start();var f2 = new foo( 100 );
f2.start();var f3 = new foo( 243 );
f3.start();
关于javascript对象模型我一直都不是太明白,不知道这样的功能在javascript中应该如何描述,望高手不吝指教!
如果这个问题确实涉及到很高深的理论,我愿意加分。function foo( c )
{
this.cnt = c;
this.start = function()
{
setInterval( DoSomething, 1000 );
}
this.DoSomething = function()
{
this.cnt++; // just do something
}
}var f1 = new foo( 1 );
f1.start();var f2 = new foo( 100 );
f2.start();var f3 = new foo( 243 );
f3.start();
function foo( c )
{
this.cnt = c;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
this.cnt++; // just do something
}
}var f1 = new foo( 1 );
f1.start();var f2 = new foo( 100 );
f2.start();var f3 = new foo( 243 );
f3.start();
{
this.cnt = c;
var me = this;
this.start = function()
{
setInterval( me.DoSomething, 2000 );
}
this.DoSomething = function()
{
me.cnt++; // just do something
//alert(me.cnt);
}
}var f1 = new foo( 1 );
f1.start();
可是为什么传入参数不能是个页面元素的id呢?
比如我写成这样: function foo( id, val )
{
this.obj = id;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}var f1 = new foo( "Text1", 1 );
f1.start();var f2 = new foo( "Text2", 100 );
f2.start();var f3 = new foo( "Text3", 243 );
f3.start();
this.DoSomething里面的这一句: window.document.getElementById(this.obj) 总是返回null;
而这一句却能成功:window.document.getElementById(this.obj).value = val;这是为什么?
感觉有点莫名其妙了!调试时用这个吧,多谢!
<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
this.obj = id;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();var f2 = new foo( "Text2", 100 );
f2.start();var f3 = new foo( "Text3", 243 );
f3.start();
}
</script>
</head>
<body onload="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>
<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
this.obj = id;
var self = this;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(function(){self.DoSomething();}, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();var f2 = new foo( "Text2", 100 );
f2.start();var f3 = new foo( "Text3", 243 );
f3.start();
}
</script>
</head>
<body onLoad="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>
不过让我见识了self.DoSomething();这样的语法,多谢!
#7是正确的。“var self = this”是为了保持上下文正确,因为setInterval是window的属性,该函数中的this指向window而不是你的foo。再就是“function(){self.DoSomething()}”方式可以很方便的传递参数或在其中直接编写要运行的代码。
顺便问一下,这样回帖能得到积分吗?我这ID已经无法下载资源了......
这样呢,直接用id.
<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
// this.obj = id;
window.document.getElementById(id).value = val;
this.start = function(id)
{
return function(){
setInterval(this.DoSomething, 1000 );//....
}
}(id);
this.DoSomething = function()
{
window.document.getElementById(id).value ++ ; // just do something
}
}function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();var f2 = new foo( "Text2", 100 );
f2.start();var f3 = new foo( "Text3", 243 );
f3.start();}
</script>
</head>
<body onload="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>