繁体   English   中英

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

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

这是我在[Javascript-优秀部分]书中找到的摘录

它根本没有用。 IE8在“ var myObject ...”行中缺少“}”,描述为错误。

我错过了什么吗?

// 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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM