0

这个片段是我在书中找到的 [Javascript - 好的部分]

它根本不起作用。在“var myObject...”行中缺少 '}',因为 IE8 描述了一个错误。

我错过了什么?

// Create myObject. It has a value and an increment
// method. The increment method takes an optional
// parameter. If the argument is not a number, then 1
// is used as the default.

var myObject = {
    value: 0;
    increment: function (inc) {
        this.value += typeof inc === 'number' ? inc : 1;
    }
};

myObject.increment(  );
document.writeln(myObject.value);    // 1

myObject.increment(2);
document.writeln(myObject.value);    // 3
4

1 回答 1

4

对象文字中,属性由逗号 ( ,) 分隔,而不是分号 ( ;)。改变这个:

value: 0;

对此:

value: 0,
于 2013-01-24T03:34:11.313 回答