I have a browser javascript that extracts data from a table in a web page and creates hidden fields in a form. Below is an excerpt of the code:
var allRows = table.childNodes;
for (var i = 1 ; i < allRows.length ; i++) {
var rowCells = allRows[i].childNodes;
var newInput = document.createElement("input");
var valueString = computeValue();
newInput.setAttribute("type", "hidden");
newInput.setAttribute("name", "timePair" + rowCells[j].getAttribute('id')) /* This line is missing a ; */
newInput.setAttribute("value", valueString);
}
As you can see, one function call is missing a semicolon, but the code is working anyway, with no errors in neither Internet Explorer nor Firefox.
I checked the javascript grammar and it looks like this shouldn't be allowed.
Why don't I get an error?
JavaScript features Automatic Semicolon Insertion :
http://jamesallardice.com/understanding-automatic-semi-colon-insertion-in-javascript/
This isn't always a good thing, for example:
return
{
foo: function() {}
}
ASI will kick in at the return line and return void. Quite a common JS gotcha... especially for those more familiar with more "formal" languages, where a missing semicolon leads to a compilation error.
Importantly:
Note that JavaScript does not treat every line break as a semicolon: it usually treats line breaks as semicolons only if it can't parse the code without the semicolons.
Quote source: Javascript: The Definitive Guide
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.