automatic-semicolon-insertion

What are the rules for JavaScript's automatic semicolon insertion (ASI)?

孤街浪徒 提交于 2020-01-16 14:22:08
问题 Well, first I should probably ask if this is browser dependent. I've read that if an invalid token is found, but the section of code is valid until that invalid token, a semicolon is inserted before the token if it is preceded by a line break. However, the common example cited for bugs caused by semicolon insertion is: return _a+b; ..which doesn't seem to follow this rule, since _a would be a valid token. On the other hand, breaking up call chains works as expected: $('#myButton') .click

I've heard Javascript inserts “;” automatically and that may cause problems [duplicate]

半腔热情 提交于 2019-12-19 05:08:14
问题 This question already has answers here : What are the rules for JavaScript's automatic semicolon insertion (ASI)? (6 answers) Closed 9 months ago . I've also heard that Go insert them too, but they followed a different approach How does Javascript insert semicolons while interpreting? 回答1: One of the biggest things I've found is, let's say you have a function that returns coordinates (or any object really), similar to this. function getCoordinates() { return { x: 10, y: 10 }; } You would

Automatic Semicolon Insertion in JavaScript without parsing [closed]

穿精又带淫゛_ 提交于 2019-12-06 05:50:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 8 months ago . I'm writing a JavaScript preprocessor which automatically inserts semicolons in places where it's necessary. Don't ask why. Now I know that the general way to tackle this problem is to write a JavaScript parser and add semicolons where necessary according to the rules in the

Automatic Semicolon Insertion in JavaScript without parsing [closed]

我的未来我决定 提交于 2019-12-04 12:04:15
Closed . This question needs to be more focused. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it focuses on one problem only by editing this post . Closed 8 months ago . I'm writing a JavaScript preprocessor which automatically inserts semicolons in places where it's necessary. Don't ask why. Now I know that the general way to tackle this problem is to write a JavaScript parser and add semicolons where necessary according to the rules in the specs. However I don't want to do so for the following reasons: I don't want to write a full

I've heard Javascript inserts “;” automatically and that may cause problems [duplicate]

↘锁芯ラ 提交于 2019-12-01 01:48:52
This question already has an answer here: What are the rules for JavaScript's automatic semicolon insertion (ASI)? 5 answers I've also heard that Go insert them too, but they followed a different approach How does Javascript insert semicolons while interpreting? One of the biggest things I've found is, let's say you have a function that returns coordinates (or any object really), similar to this. function getCoordinates() { return { x: 10, y: 10 }; } You would expect to get back an object right? WRONG! You get back undefined. The interpreter converts the code into this function getCoordinates(

Automatic semicolon insertion & return statements [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 13:50:17
This question already has an answer here: What are the rules for JavaScript's automatic semicolon insertion (ASI)? 5 answers As you might know, ECMAscript tries to be smart and will automatically insert semicolons if you didn't write those explicitly. Simple example function foo() { var bar = 5 return bar } will still work as expected. But there are some caveats if you rely on that. If we re-write that function like so function foo() { var bar = 5 return { bar: bar } } ..that function would now return undefined because the interpreter would insert that semicolon right after the return

No semicolon before [] is causing error in JavaScript

萝らか妹 提交于 2019-11-26 08:32:22
问题 var a = [1, 2, 3, 4]; var b = [10, 20, 30, 40]; console.log([a, b].length)[a, b].some(function(x) { x.push(x.shift()) }); I was extremely surprised today when this code caused [a,b].some(function(x){ x.push(x.shift()) }); ^ TypeError: Cannot call method \'some\' of undefined Obviously the JavaScript \'auto semicolon insertion\' is not working as expected here. But why? I know you might recommend to use ; everywhere to avoid something like that, but the question is not about whether it is

Automatic semicolon insertion & return statements [duplicate]

此生再无相见时 提交于 2019-11-26 03:44:23
问题 This question already has answers here : What are the rules for JavaScript's automatic semicolon insertion (ASI)? (5 answers) Closed 4 years ago . As you might know, ECMAscript tries to be smart and will automatically insert semicolons if you didn\'t write those explicitly. Simple example function foo() { var bar = 5 return bar } will still work as expected. But there are some caveats if you rely on that. If we re-write that function like so function foo() { var bar = 5 return { bar: bar } }

What are the rules for JavaScript's automatic semicolon insertion (ASI)?

拜拜、爱过 提交于 2019-11-25 22:54:19
问题 Well, first I should probably ask if this is browser dependent. I\'ve read that if an invalid token is found, but the section of code is valid until that invalid token, a semicolon is inserted before the token if it is preceded by a line break. However, the common example cited for bugs caused by semicolon insertion is: return _a+b; ..which doesn\'t seem to follow this rule, since _a would be a valid token. On the other hand, breaking up call chains works as expected: $(\'#myButton\') .click