3 ms·
There is also the function scope vs block scope... var x = 3 will escape the latter.
by egnehots 2y ago
There is also the function scope vs block scope...
var x = 3 will escape the latter.
- jolmg 2y agoIf you mean the following, it actually doesn't: (function () { var x = 0; const foo1 = function(a, b) { var x = 2; } const foo2 = (a, b) => { var x = 3; } foo1(); console.log(x); // prints 0 foo2(); console.log(x); // prints 0 })() However, there is the difference in how the implicit semicolons are inserted: const foo1 = function(a, b) { return a + b; } (2, 3) console.log(foo1) // prints 5 const foo2 = (a, b) => { return a + b; } (2, 3) console.log(foo2) // prints [Function: foo2]
- olliej 2y agohaha, oh ASI - I was very confused by your example as I read this as if it was > const foo1 = function(a, b) { return a + b; } (2, 3) > console.log(foo1) // prints 5 > const foo2 = (a, b) => { return a + b; } (2, 3) > console.log(foo2) // prints [Function: foo] Even though it makes no sense for (2, 3) to be a result in those cases, that was just how I ended up reading it, and I was exceptionally confused about how the printed output could possibly happen. A super nice example of how subtle differences can really change things though. As a side note, ASI for JS is actually super easy to implement and the rules are actually really simple (leaving aside whether the feature itself is good :D ) as it's just "these specific statements can have a new line instead of a semicolon" - so in the parser instead of consume(semicolon) you can just do "semicolon or newline" (You can check the logic in JSC in https://github.com/WebKit/WebKit/blob/main/Source/JavaScriptCore/parser/Parser.cpp https://github.com/WebKit/WebKit/blob/main/Source/JavaScript... - just look for autoSemicolon() or autoSemi() I can't recall off the top of my head)
- Am4TIfIsER0ppos 2y ago> implicit semicolons are inserted lmao what a p
- culi 2y agoI believe this is actually a difference between named and anonymous functions. The named function syntax is function foo1() { ... } Both of the below examples are anonymous functions const foo2 = function() { ... } const foo3 = () => { ... }