2010-12-08 94 views
3
var rooms = { 
bedroom: { 
    info: "A dusty bed lies sideways in the midle of the room"; 

    north: function () { 
     //this function returns an error 
    } 
} 
}; 

我不能明白为什么这会返回一个意外的标识符简单的JavaScript,功能

- 编辑 感谢另一个问题

JavaScript中的好的部分,他有

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

和我在做什么不一样?

+0

@fxmile - 看起来像书中的错误。它是在第28页(或关闭)?该页面的[勘误表中]列出了类似的内容(http://oreilly.com/catalog/errata.csp?isbn=9780596517748)。 – user113716 2010-12-08 21:35:57

回答

5

在定义键和值来分隔它们时,应该在对象文字内使用,,而不是;

var o = { name: 'john', age: 13 } 
2
the room"; 

应该有,,不;

2

要回答你的第二个问题,它看起来在书中有一个错字。

不正确的例子是:

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

正确的例子是:

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

注意就行了value: 0,逗号。

正如其他人所提到的,应该使用逗号(而不是分号)作为对象文字。