2010-12-08 96 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,逗號。

正如其他人所提到的,應該使用逗號(而不是分號)作爲對象文字。