-2

Having read about JS objects I believe I can adds methods in the following way:

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"; 
    }   
}

};

When I run this through JSFiddle it says

Expected '(end)' instead saw ':'.

Next to the line with "colour" in it.

I'm not sure what I have done wrong?

4

1 回答 1

5

您只是缺少一个逗号来分隔alarmand colour

},  // <--- here
colour: function (x) {

和你在 and 之间finish的一样alarm

finish: null,
alarm: function (x) {

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

于 2013-11-05T21:02:12.300 回答