1
var foo = {
    bar: function() { return ""; }
}; 
foo:bar();

The above results in:

Uncaught ReferenceError: bar is not defined

Using it in this context console.log(foo:bar()); results in:

SyntaxError: missing ) after argument list

In some cases object colon object / function does SyntaxError but in this case it doesn't, why is this the case? I have tested this in both node.js and a couple browsers and this behavior is consistent, so I was curious as to what would not produce either error in the context of "foo:bar".

Don't remember how else I achieved a SyntaxError, but it had something to do with calling a function from an object using :

benvc
  • 14,448
  • 4
  • 33
  • 54
ACSD_
  • 13
  • 4
  • `In some cases` Might be an idea if you show both cases.. – Keith Sep 28 '18 at 17:26
  • thanks for the edit help, but ReferenceError happens at foo:bar SyntaxError missing ) after argument happens with console.log(foo:bar()); – ACSD_ Sep 28 '18 at 17:41
  • Refer to https://stackoverflow.com/questions/9384865/javascript-colon-for-labeling-anonymous-functions for more on colon syntax – gurpreet singh chahal Sep 28 '18 at 17:41
  • 1
    Think you meant "SyntaxError" instead of "SymbolError" (but maybe there really is some sort of symbol error type that I have never run into). – benvc Sep 28 '18 at 17:45
  • @benvc I think I saw one JS parser somewhere that produced `SymbolError` which was instead of `SyntaxError`. I can't remember where or even *what* that was, though. It could have been a tool that reads JS files and processes them but isn't JS itself, so a `SymbolError` might have come from whatever programming world it was coming from. – VLAZ Sep 28 '18 at 17:51
  • 1
    i did mean "SyntaxError" my brain just was stuck on what i was expecting to happen in another language. its surprising javascript doesn't differentiate the "SyntaxError" "types" – ACSD_ Sep 28 '18 at 17:52

1 Answers1

3

I'm not sure what you wanted to achieve with this.

Having it as "foo:bar" it has nothing related to foo variable. Instead it declares label with name of 'foo'.

Declaring label inside of console.log leads to SyntaxError since it break parsing the statesment.

On the other side having it without console.log as you mentioned has correct syntax: it declares label 'foo' and tries to call the function 'bar' that does not exist. That's why it shows ReferenceError.

Anyway Javascript expects dot(.) to access object members: foo.bar()

skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • thanks, i was working on lua before this an accidentally did that. the console spit out a reference error at foo:bar(); instead of a syntax error and i was wondering why – ACSD_ Sep 28 '18 at 17:48
  • I see. I was not sure if you wanted just call the object method or do something more specific like static member call or something else(that could be if you went from other language - but I did not thing about lua) – skyboyer Sep 28 '18 at 17:58
  • in lua `object:methodcall()` forces first argument to be the object just a habit i was in when differentiating between calls like `math.floor` and `car:drive`, if you called `car.drive` here drive would be completely unaware of the specific car object – ACSD_ Sep 28 '18 at 20:01