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
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.
Any language that doesn’t rely on semicolons (but instead on newlines) to delimit statements potentially allows this. Consider Python:
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:
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: