向JavaScript对象添加多个方法

Rus*_*ell -2 javascript methods

阅读了JS对象后,我相信我可以通过以下方式添加方法:

var activity = {
name: null,
start: null,
finish: null,
alarm: function (x) {
    if (x === this.start) {
    return true;
    }
    if (x !== this.start) {
    return false;
    }
}       
colour: function (x) {
    if (x < this.start) { 
    return "red"; 
    }
    if (x > this.start && x < this.finish) { 
    return "green"; 
    }
    if (x > this.finish) { 
    return "grey"; 
    }   
}

};
Run Code Online (Sandbox Code Playgroud)

当我通过JSFiddle运行时,它说

预期'(结束)'反而看到':'.

旁边有"颜色"的行.

我不确定我做错了什么?

Jon*_*ski 5

你只是错过了一个逗号来分开alarmcolour:

},  // <--- here
colour: function (x) {
Run Code Online (Sandbox Code Playgroud)

与你之间的相同finishalarm:

finish: null,
alarm: function (x) {
Run Code Online (Sandbox Code Playgroud)

对于Object文字,key:value即使整数,也总是需要逗号来分隔对function.