2011-11-05 33 views
1
var testValue = "This is the Cookbook's test string"; 
var subsValue = "Cookbook"; 
var iValue = testValue(subsValue); // returns value of 12, index of substring 
alert(iValue); 

我正在通過Shelley Powers編寫Javascript食譜。但是我嘗試了這段代碼,我無法獲得12.這沒什麼!而且我在O'Reilly的勘誤頁面找不到正確的部分。我可以使用括號來獲取Javascript中的子串位置嗎?

圓括號真的有用嗎?我知道String對象的indexOf方法必須工作。 Like:

var testValue = "This is the Cookbook's test string"; 
var subsValue = "Cookbook"; 
var iValue = testValue.indexOf(subsValue); 
alert(iValue); // This time I got the alert 12. 
+0

顯然,這是一個打字錯誤。 – simplyharsh

回答

0

不,這段代碼絕對不應該工作。甚至不應該在沒有TypeError的情況下執行(試圖像調用函數一樣調用字符串)。

如果在本書中輸入的內容是錯誤的。

0

我懷疑它,看起來像是出版物中的一個錯誤,我會報告它,因爲它不在勘誤中。

var iValue = testValue(subsValue); # => TypeError: string is not a function 
相關問題