我的 JS 代码有问题。我必须用对象构建一个闹钟,但它不起作用,即使在我看来很好。 这是:
function AlarmClock(){
this.hour;
this.minutes;
this.Hours=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
this.currentTime=
function(h, m){
this.hour=h;
this.minutes=m;
if (this.hour>23){
this.hour=0;
}
if(this.minutes>59){
this.minutes=0;
}
}
this.alarm=
function(h, m){
this.hour=h;
this.minutes=m;
if(this.hour>23){
this.hour=0;
}
if(this.minutes>59){
this.minutes=0;
}
}
this.tic=
function(){
var i=0;
while(i<=m){
m.currentTime+=i;
if(m.currentTime==60){
h.currentTime+=1;
}
if (h.currentTime==24){
h.currentTime=0;
}
if(((h.currentTime)&&(m.currentTime))==((h.alarm)&&(m.alarm))){
return ("Time to wake up!");
}else{
i++;
}
}
}
function foo(){
var c=new AlarmClock();
c.currentTime(13, 0);
c.alarm(13, 2);
}
foo();
我在两个不同的环境中尝试了两次:
- 第一个 JS 环境,( http://www.di.unipi.it/~mezzetti/easyjs2/ ) 给我一个
“SyntaxError: Unexpected end of input”
(也许是因为我犯了一些错误) - 另一方面,第二个 ( http://www.squarefree.com/jsenv/ ) 给我这个错误
“第 0 行错误:脚本错误。”
我怎样才能跨过这个?
非常感谢:D
最佳答案
函数 AlarmClock()
缺少最后一个右大括号 }
。只需在函数 foo()
上方放置一个 }
即可。
关于javascript - JS环境第0行脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27020521/