Javascript 方法调用模式示例不起作用,为什么

Javascript The Method Invocation Pattern Sample is not working, why?

本文关键字:不起作用 为什么 方法 调用 模式 Javascript      更新时间:2023-09-26

这个片段是我在书中找到的[Javascript - the good parts]

它根本不起作用。在"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

在对象文字中,属性用逗号 ( , 分隔,而不是分号 ( ; )。更改此设置:

value: 0;

对此:

value: 0,