[Tech] Javascript...

Solemn

New Member
Just been looking through some code and i am trying to work out the order this would run in.



if (m_discard==true) f_discard_row("no")
_CreatePage(m_currentAlias);
m_discard=false;



the if statement has no {} so not sure what is part of the if statement and would is outside of it.

Also the function f_discard_row has no closing ; so I'm not even sure why this is working...

I'm reading this as a C developer so not that clued up on how lenient JS is


Thanks for any advice !
 

Wol

In Cryo Sleep
It should be the same as if it were C, where you have an if statement without braces:

e.g. you can do

if(herp==derp)
for(i = 0; i < 5; i++)
{
dosomething();
}

and it just calls the single statement after
 

bacon

Well-Known Member
Javascript doesn't require semi-colons, it interprets where it thinks they should be. In this case it's using this definition:
When the program contains a token that is not allowed by the formal grammar, then a semicolon is inserted if (a) there is a line break at that point, or (b) the unexpected token was a closing brace.
so it's creating a semi-colon after the closing brace of that function call.

This gives you the equivalent:
Code:
if (m_discard==true) {
  f_discard_row("no");
}
_CreatePage(m_currentAlias);
m_discard=false;
 

Ronin Storm

Administrator
Staff member
Yeah, just to confirm what bacon said:

JS is lazy about semi-colons (but you should try not to be).

Also, JS works in a C-like fashion with if statements where single line statements apply only to that line (and thus should be used sparingly to avoid ambiguity for the maintenance programmer).

Also, in many browsers, a syntax problem in a JS function can cause a silent failure if not otherwise caught. Be prepared to go hunting with alert() ...
 

thatbloke

Junior Administrator
I'm making heavy use of JS with the QML (Qt extension) code that I'm writing at the moment. Something I'm very hot on with my team is code formatting - just because JS allows you to be a bit lazy in certain situations, does not mean you should be when writing it. It will avoid situations precisely as you have described here.
 

Wol

In Cryo Sleep
I'm making heavy use of JS with the QML (Qt extension) code that I'm writing at the moment. Something I'm very hot on with my team is code formatting - just because JS allows you to be a bit lazy in certain situations, does not mean you should be when writing it. It will avoid situations precisely as you have described here.

It's kinda like learning VB......

:|
 
Top