Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does any other language other than JavaScript have a difference between brace start locations (same line and next line)?

Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one interesting thing (page 27 for reference).

In Javascript, in some cases, there is a difference if the brace start location is different.

function test_function1() {     return     {         name: 'rajat'     }; }  var obj = test_function1(); alert(obj);  //Shows "undefined" 

While

function test_function2() {     return {         name: 'rajat'     }; }  var obj = test_function2(); alert(obj); //Shows object 

JSfiddle Demo

Does any other language out there have such behavior? If so, then I would have to change my habit for sure..:)

I am mainly concerned about PHP, C, C++, Java, and ruby.

like image 468
Rajat Singhal Avatar asked Feb 08 '12 10:02

Rajat Singhal


People also ask

What is difference between JavaScript and other languages?

JavaScript is a cross-platform language, whereas Java is not. Prior to execution on the client, Java needs to be compiled on the server whereas JavaScript is interpreted by the client end. JavaScript is a dynamic language and Java is a static language. JavaScript aims on creating interactive web pages.

Is JavaScript similar to other languages?

In a nutshell, JavaScript is a dynamic scripting language supporting prototype based object construction. The basic syntax is intentionally similar to both Java and C++ to reduce the number of new concepts required to learn the language.

Which language is most like JavaScript?

If you don't mind a syntactic departure, both Ruby and Python are dynamic languages like JavaScript, and both are fairly popular these days for desktop apps (at least on Linux). You certainly could use JavaScript if you want to.

How fast is JavaScript compared to other languages?

However, today I made a benchmark script to compare the speed of floating point calculations in the two languages and the result is amazing! JavaScript appears to be almost 4 times faster than C++! I let both of the languages to do the same job on my i5-430M laptop, performing a = a + b for 100000000 times.


2 Answers

Any language that doesn’t rely on semicolons (but instead on newlines) to delimit statements potentially allows this. Consider Python:

>>> def foo(): ...   return ...   { 1: 2 } ...  >>> def bar(): ...   return { 1: 2 } ...  >>> foo() >>> bar() {1: 2} 

You might be able to construct a similar case in Visual Basic but off the top of my head I can’t figure out how because VB is pretty restrictive in where values may be placed. But the following should work, unless the static analyser complains about unreachable code:

Try     Throw New Exception() Catch ex As Exception     Throw ex.GetBaseException() End Try  ' versus  Try     Throw New Exception() Catch ex As Exception     Throw     ex.GetBaseException() End Try 

From the languages you mentioned, Ruby has the same property. PHP, C, C++ and Java do not simply because they discard newline as whitespace, and require semicolons to delimit statements.

Here’s the equivalent code from the Python example in Ruby:

>> def foo >>   return { 1 => 2 } >> end => nil >> def bar >>   return >>   { 1 => 2 } >> end => nil >> foo => {1=>2} >> bar => nil 
like image 164
Konrad Rudolph Avatar answered Sep 29 '22 15:09

Konrad Rudolph


The JavaScript interpreter automatically adds a ; at the end of each line if it doesn't find one (with some exceptions, not getting into them here :).

So basically the issue is not the braces' location (which here represent an object literal, not a code block as in most languages), but this little "feature" that forces your first example to return ; => undefined. You can check out the behavior of return in the ES5 spec.

For other languages that have similar behavior, check out Konrad's answer.

like image 32
Alex Ciminian Avatar answered Sep 29 '22 15:09

Alex Ciminian