Re:ImmortalDB – A resilient key-value store for the browser
by recursive on Monday January 28, 2019 @03:43PM
(#19020890)
Can anyone explain why there's a leading semi-colon in the inline script example? AFAICT it doesn't do anything.
[ Parent
| on HN ]
Re:ImmortalDB – A resilient key-value store for the browser
by daleharvey on Monday January 28, 2019 @03:49PM
(#19020930)
Some people for some reason write javascript without semicolons, if they do that you cant safely concatenate javascript files together since the file2 may modify file1's last expression, putting a ; at the start guarantees to complete the last files expression if they havent already done so. A bunch of concatenators will do this automatically / by default
Re:ImmortalDB – A resilient key-value store for the browser
by jacobwg on Monday January 28, 2019 @04:00PM
(#19021035)
You are correct that this is an issue when writing JavaScript without semicolons, but it's also an issue if you place an IIFE (immediately invoked function expression) directly after a function, even if you're using semicolons:
function hasSemicolons() {
console.log('hello world');
}
(function() {
console.log('this is broken, JS will call hasSemicolons()');
})()
In the ImmortalDB example, since it's a standalone <script> tag this is just extra defensive against a minifier that would join the tag with other tags, but just wanted to call out that there's a reason to include the leading semicolon in front of an IIFE whether or not you write your JavaScript with semicolons.See also: https://stackoverflow.com/questions/1873983/what-does-the-le...
Re:ImmortalDB – A resilient key-value store for the browser
by arilotter on Monday January 28, 2019 @11:36PM
(#19023804)
Your snippet doesn't fail.
This one does
console.log('this doesn't work!')
(false ? console.log : console.error)('this will fail with a TypeError')
>> Uncaught TypeError: console.log(...) is not a function
console.log('this works!')
;(true ? console.log : console.error)('hello world')
Re:ImmortalDB – A resilient key-value store for the browser
by recursive on Monday January 28, 2019 @06:46PM
(#19022289)
So I tested this out in Firefox. I think you're wrong. I never see 'hello world' even if I remove all the whitespace between the functions.
Re:ImmortalDB – A resilient key-value store for the browser
by munk-a on Monday January 28, 2019 @06:20PM
(#19022068)
There is a reason why that issue with IIFEs can be hit, and it's because you've omitted some semicolons - granted they are the sort that most people omit.
Since everything in js is a statement the correct semi-colon syntax for the above is
function hasSemicolons() {
console.log('hello world');
};
(function() {
console.log('this is broken, JS will call hasSemicolons()');
})();
Re:ImmortalDB – A resilient key-value store for the browser
by recursive on Monday January 28, 2019 @06:51PM
(#19022314)
The ECMAScript specification (https://www.ecma-international.org/publications/files/ECMA-S...) has statements in a separate chapter from function declarations. (Chapter 14) Furthermore, the function declaration grammar does not contain a semi-colon, unlike other statements.
Re:ImmortalDB – A resilient key-value store for the browser
by kevan on Monday January 28, 2019 @03:52PM
(#19020967)
When you're dealing with JS files it makes sense, but the example is an inline <script> tag. It might be an abundance of caution just in case you're dealing with CDN optimizers that slice and dice inline scripts. I've experienced that with Akamai a few times.