I know this might be a dumb question, but i spent quite some time and still cannot figure it out.
For sake of brevity, i created a simple demo:
$(function() {
alert('hi, jQuery!')
(function() {
console.log('hi, IIFE')
})()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The alert
work as expected, but console.log
failed. More strangely, the error message logs:
Uncaught TypeError: alert(...) is not a function
Why did this even happen, as the alert
statement was already executed?
Update
This question is due to lacking knowledge of how JS auto semicolon insertion
(a.k.a ASI) works.
Refer JavaScript Semicolon Insertion - Everything you need to know for more detail.