SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
427
Reaction score
174
First Language
Dutch
Primarily Uses
RMMV
I usually see this:

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this);}; <<< ;But this also works just fine:

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this);} // <<< removed ;What's the difference? Which one is better and why?
 
Last edited by a moderator:

Mellye

Veteran
Veteran
Joined
Oct 24, 2015
Messages
347
Reaction score
279
First Language
Portuguese
You don't need semi-colons in JavaScript. You could have zero semi-colons in your entire code and it would still work, most parsers will read just fine (I've not tested this on RMMV, but I figure it does too). The issue is that in some specific situations, the parser may interpret that something is part of the same statement when you didn't intend it to be so.

For the sake of consistency and clarity, you do should use semi-colons at the end of every statement, despite the fact that you're not required to do so.

Now, if you're used to method declarations in other languages, you might find it weird to have a semi-color after that. But there's a different in how we're creating methods here.

In some other language, you might have:

public String Foo(){    return "bar";}Those declarations indeed do not have a semi-colon at their end.

But in JavaScript, we're assigning a function to a variable.

Just like this would have a semi-colon:

Scene_MiniMap.prototype.initialize = "foo_bar";So does this:

Scene_MiniMap.prototype.initialize = function();And therefore so does this:

Code:
Scene_MiniMap.prototype.initialize = function() {    Scene_MenuBase.prototype.initialize.call(this);};
 
Last edited by a moderator:

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,302
First Language
Binary
Primarily Uses
RMMZ
Using a semicolon at the end of each statement also helps when trying to 'minify' your code. Cant really be done otherwise! :)
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
32,162
Reaction score
8,017
First Language
German
Primarily Uses
RMMV
What's the difference? Which one is better and why?
A lot depends on the parser or compiler and the exact language used for the code.
Allowing the programmer to forget the ; makes for easier to write code that is also a bit more difficult to parse and read.


Other languages are not as forgiving in those cases, and that is for a good reason - but it can also cause problems if the programmer is sloppy.


There was a NASA mission several decades ago that failed because one of the programmers forgot a single ; in the pascal code used, and that changed how the code was processed...
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,302
First Language
Binary
Primarily Uses
RMMZ
There was a NASA mission several decades ago that failed because one of the programmers forgot a single ; in the pascal code used, and that changed how the code was processed...
Is that true? Thats an EPIC failure!!! OMG...
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,862
First Language
English
Yes, you should add semi-colons.


Javascript compilers will try to do it for you, but they can get it wrong, and the results may be devastating.


I have run into cases where not adding semi-colons caused MV to not parse my script correctly.
 
Last edited by a moderator:

Ghost of Christmas Kloe

The Icecream Princess
Veteran
Joined
Nov 15, 2015
Messages
1,548
Reaction score
960
First Language
English
Primarily Uses
RMMZ
There was a NASA mission several decades ago that failed because one of the programmers forgot a single ; in the pascal code used, and that changed how the code was processed...
Wait? NASA uses Javascript to fly rockets? Woah! 
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
427
Reaction score
174
First Language
Dutch
Primarily Uses
RMMV
I know it's just a variable asignment in Javascript. But to me it's a method declaration and method declarations imo should not have semi-colons at the end.

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this); // << this one okay}; // << this one... Feels wrong. But if I leave it out, it could maybe in theory someday be interpreted wrongly...Damnit so I should put them behind every method as well then..
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,548
Reaction score
3,302
First Language
Binary
Primarily Uses
RMMZ
A lot of code languages, such as C/C++ use ; to separate each statement form the next. Its the common separator. :)
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
866
First Language
German
Primarily Uses
Pascal is a different programming language.

Personally, i do it like i've done in Java for a long time, which is adding a ; after every statement except if and while (but after do-while).

I know it's just a variable asignment in Javascript. But to me it's a method declaration and method declarations imo should not have semi-colons at the end.

Code:
Scene_MiniMap.prototype.initialize = function() {    Scene_MenuBase.prototype.initialize.call(this); // << this one okay}; // << this one... Feels wrong. But if I leave it out, it could maybe in theory someday be interpreted wrongly...
Damnit so I should put them behind every method as well then..
It's assigning a variable to an anonymous function, so i would put a ; there. Now, if you would write
Code:
function abc() {}
I wouldn't add a ;
 
Last edited by a moderator:

Mellye

Veteran
Veteran
Joined
Oct 24, 2015
Messages
347
Reaction score
279
First Language
Portuguese
I know it's just a variable asignment in Javascript. But to me it's a method declaration and method declarations imo should not have semi-colons at the end.

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this); // << this one okay}; // << this one... Feels wrong. But if I leave it out, it could maybe in theory someday be interpreted wrongly...Damnit so I should put them behind every method as well then..
An example of a situation where it could be interpreted differently from what you'd want to:

Scene_MiniMap.prototype.initialize = function() {    Scene_MenuBase.prototype.initialize.call(this);}(function() {    //do some other stuff not related to the above})();If you don't have a semi-colon after that first function declaration, I think the parser will likely interpret the second function as being a parameter to the implicit call of the first function and then the last (); will fail with "is not a function". Certainly not what you wanted.
 
Last edited by a moderator:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,594
Reaction score
3,813
First Language
Java's Crypt
Primarily Uses
RMMZ
If something is an expression (you're running code) add a semi-colon. If you are declaring something, don't add it. Anything that uses an optional curly bracket should not have semi-colons either:

Code:
function x(){}x = function() {};if (x) {}while (x){}x();
 

kiriseo

Veteran
Veteran
Joined
Oct 27, 2015
Messages
245
Reaction score
83
First Language
German
I know it's just a variable asignment in Javascript. But to me it's a method declaration and method declarations imo should not have semi-colons at the end.

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this); // << this one okay}; // << this one... Feels wrong. But if I leave it out, it could maybe in theory someday be interpreted wrongly...Damnit so I should put them behind every method as well then..
The semi-colon after your function declaration is indeed for the assignment and has nothing to do with the declaration of the function.

It's like you would do:

function a { // do something }Scene_Minimap.prototype.initialize = a;It's virtually the same just in one single step.

If you don't assign a function to a variable, you don't need the semi-colon after the closing brackets.
 
Last edited by a moderator:

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
I usually see this:

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this);}; <<< ;But this also works just fine:

Scene_MiniMap.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this);} // <<< removed ;What's the difference? Which one is better and why?
JavaScript has ASI (Automatic Semicolon Insertion), the set of rule where ASI applies are not exactly straightforward for someone that start using JS,

it's possible to avoid semicolon in most cases, basically the rule is that if the parser can understand you intended to break the line there for a reason it inserts the semicolon;

If the new line can continue the previous one then you have a trouble,

mostly this means that if you always use brackets {} you just have to avoid starting a line with a ( or a [ to be safe, for example:

(function(){ // ...})['a','b','c].forEach(function(){ // ...})is probably not what you want as the first function expression is followed by a sequence expression (a sequence of stuff with comma, this means that everything between comma gets executed and the last value is returned), so at some point your JS will look like this for your interpreter:

(function(){})['c'].forEach(function(){})so JS will try to see if function(){} has a 'c' property and if so it will evaluate it, then try to run forEach on the result.

let's suppose you have something like 

Function.prototype.c = [1,2,3];

before that expression this means that JS will manage to execute the forEach but on the 1,2,3 sequence instead of the 'a','b','c' that you would expect

so bottom line, put semicolon where needed, if not sure put semicolon at the end of any line that can end with semicolon until you get the rule better,

however when working in project where semicolon everywhere is agreed just stick to the rule :)

EDIT:

this site seems to have the ruleset explained better (and explains the possible pitfalls as well):

http://inimino.org/~inimino/blog/javascript_semicolons
 
Last edited by a moderator:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,594
Reaction score
3,813
First Language
Java's Crypt
Primarily Uses
RMMZ
If you're not sure, use jslint and add a semi-colon where it says you should and remove the semi-colon where it says you should :)
 

kentaromiura

Veteran
Veteran
Joined
Nov 1, 2015
Messages
46
Reaction score
26
First Language
English
Primarily Uses
Using a semicolon at the end of each statement also helps when trying to 'minify' your code. Cant really be done otherwise! :)
This is not true anymore, any good minifier nowadays transform your code in an AST and when doing so any not meaningful semicolon is just ignored, this may have been true in the past when people just concat together different files and sometime one ended in an expression and the following started with ( or [, most likely the first as people use to wrap their code in a IIFE.
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
This is not true anymore, any good minifier nowadays transform your code in an AST and when doing so any not meaningful semicolon is just ignored, this may have been true in the past when people just concat together different files and sometime one ended in an expression and the following started with ( or [, most likely the first as people use to wrap their code in a IIFE.

this ^

How ever I have been reading some articles that leaving out the ; can cause issues, especially if you concat your scripts. though there is no actual proof, just issues on github people have found
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
427
Reaction score
174
First Language
Dutch
Primarily Uses
RMMV
Why does nobody put a ; behind the i++? Shouldn't it also have one?

Code:
for(var i=0; i<someArray.length; i++){}
 
Last edited by a moderator:

Latest Threads

Latest Posts

Latest Profile Posts

zomboars.jpg

Concept art

Zomboars- main enemies in my story
mutant pigs addicted to beer
SF_People1_3 added!
index.php

I haven't been on here in forever wow(missed my forum birthday T-T). Anyways, I'm dabbling in BGMs, here's something I'm actually willing to share.

Binge-watched 6 episodes of the Erased anime... And that means I've watched half of it?! Sigh...

Forum statistics

Threads
109,203
Messages
1,043,010
Members
141,709
Latest member
chonkycat
Top