开发者

Understanding ECMAScript implicit semicolons and whitespace parsing

开发者 https://www.devze.com 2023-03-09 08:30 出处:网络
I\'ve seen, very often in fact, this cited as why to use K&R style when writing ECMAScript. function foo () {

I've seen, very often in fact, this cited as why to use K&R style when writing ECMAScript.

function foo () {
  return
    {
      foo: 1
    }
  ;
}

That doesn't work in ECMAScript or Javascript: implicit-semicolon addition results in the function returning undefined. However I see this all the time too

function bar () {
  var a = "BAR";
  return a
    .toLowerCase()
  ;
}

And, I'm wondering why implicit se开发者_JAVA百科micolons doesn't result in that returning "BAR", why does bar get returned there?


Because the syntax doesn't work with an implicit semicolon at the end of the line.

If you add the semicolon:

function bar () {
  var a = "BAR";
  return a;
    .toLowerCase()
  ;
}

you would get a syntax error on the next line.

0

精彩评论

暂无评论...
验证码 换一张
取 消