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);
I’m working on the Javascript Cookbook by Shelley Powers. But I try this code snippet and I cannot get 12. It’s nothing! And I cannot find the correct section in O’Reilly’s errata page.
Do parentheses really work? I know the indexOf method of the String object must work.
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.
No, that code definitely shouldn’t work. Shouldn’t even execute without a TypeError (for trying to invoke a string as though it were a function).
If it’s typed like that in the book it’s an error.