Weird Unexpected token with newlines

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
This does not work

MM_POI.prototype.makeSaveContents = function(){    return    {        id        :this.id,        name      :this.name // <<< SyntaxError: Unexpected token :    };};This does:

MM_POI.prototype.makeSaveContents = function(){ return { id :this.id, name :this.name };};The only difference is 1 line vs several lines. No difference in code. I even kept the spaces intact. Why is this?
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Are you sure the object is built correctly in the example that works? If it does, my guess is that it's because you don't have the opening { on the same line as 'return'.

Also, why do you have the colons all the way over to the values? Typically you leave them with the ID.
 

GaryCXJk

Veteran
Veteran
Joined
Dec 24, 2012
Messages
88
Reaction score
46
First Language
Dutch
Primarily Uses
Are you sure the object is built correctly in the example that works? If it does, my guess is that it's because you don't have the opening { on the same line as 'return'.

Also, why do you have the colons all the way over to the values? Typically you leave them with the ID.
That is exactly what the problem is, and the problem with JavaScript automatically adding semicolons implicitly.

In any other languages, you have to add semicolons to define the end of a line. In JavaScript, oftentimes it adds those semicolons itself. This means that in other languages you can easily multiline things, while in JavaScript you have to be very careful with that.
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
Are you sure the object is built correctly in the example that works?
100%. Tested it several times, checked all values.

The colons are so far over because there used to be many more values. But I removed them to find out what was wrong. I thought I had some spelling mistake or a lower/uppercase bug. But nope it was the newline.

So basically, multi-lining in Javascript is also very dangerous? Pfff... Wish we could use c++/c# or something decent. Ruby wasn't great but Javascript really has me baffled so now and then. Strict languages are the best. Just the amount of times I had to debug something only to find out I had an undefined value or used an unexisting variable-name or forgot an uppercase character, but Javascript is okay with all that...

Okay I'll stick to single-line returns then :( . I'll just make a very very very long single-line-return... <insert very disappointing emoticon here>
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
MM_POI.prototype.makeSaveContents = function(){ return { id: this.id, name: this.name };};thats why
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top