0

I am asking this because in AngularJS, we can do

$routeProvider

.when("/", {
    templateUrl: "pages/main.html",
    controller: "mainController"        
})

.when("/second", {
    templateUrl: "pages/second.html",
    controller: "secondController"            
})

and similarly, we can do

bar = {
    value: 123,
    print: function() {
        console.log(this.value)
    }
}

bar

.print()

and it will be invoked as bar.print() But on the other hand, the following:

function foo() {

    return
    {
      a: 123
    }

}

console.log(foo())

will have a subtle error of JavaScript interpreter inserting a semicolon after the return, and therefore treating it as

return;      // <-- returning nothing, which means undefined
{
    a: 123
}

and one more case is:

a = 3

+4

console.log(a)

a = 3;

+4

console.log(a)

The first a will actually take it as 3 + 4, while the second case of a, it can take the +4 as a statement that evaluates to 4 and do nothing.

Why will a semicolon be inserted this way, while at the beginning of this question, that code has 2, 3, or more blank lines, but the . can still connect back to the object? What rule is governing this?

Sample at:
https://jsfiddle.net/g1gsnmfr/5/

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 2
    Your question makes it seem like your Angular examples aren't JavaScript and act differently from JavaScript. The only difference between the two examples you gave is where the line breaks are. The line break after `return` is significant in terms of ASI. The line break after `bar` isn't, not if it's followed by something that can continue the expression according to the rules of ASI. Nothing to do with Angular. – T.J. Crowder Jan 27 '16 at 14:38
  • that's why I showed a second example to show that in plain JavaScript we can do the same, meaning nothing special in AngularJS – nonopolarity Jan 27 '16 at 14:42
  • So if the question has nothing to do with Angular, what confuse matters by bringing Angular into it? – T.J. Crowder Jan 27 '16 at 15:43
  • just to have some perspective why you would want to make the dot so far away. Otherwise you naturally won't separate `obj.show()` – nonopolarity Jan 27 '16 at 23:10

0 Answers0