win = Window.new()
.setTitle("A new window")
.setForeground(Red)
.setBackground(White)
.setMenuBar(fileMenu, viewMenu, helpMenu);
I think this would be uglier and more error-prone if instead you had to use \ or some other character to indicate they are all part of the same expression: win = Window.new()\
.setTitle("A new window")\
.setForeground(Red)\
.setBackground(White)\
.setMenuBar(fileMenu, viewMenu, helpMenu); myLongList = [1, 2, 3,
4, 5, 6,
7, 8, 9]
No continuation character is needed, because the continuation is implied.However, I can't think of a direct parallel to your example above. It looks handy. What API/language are you alluding to?
I guess the Pythonic way would be something like:
win = Window(title = "A new window",
foreground = "Red",
background = "White",
menuBar = [fileMenu, viewMenu, helpMenu])
But I disgress. :^) win = Window.new({title: "A new window",
foreground: Red,
background: White,
menuBar: [fileMenu, viewMenu, helpMenu]});As i said before: no particular language.
win = new Window().
setTitle("A new window").
setForeground(Red).
setBackground(White).
setMenuBar(fileMenu, viewMenu, helpMenu); function getIndex() {
return 0
}
function f1() {
alert("foo")
}
function f2() {
alert("bar")
}
var intIndex = getIndex()
[f1, f2][intIndex]()
It looks like it should alert "foo", but run it and you get an "intIndex is undefined" error. Good luck debugging that!The problem is, the javascript rules for continuing lines are that whenever the next character is any of [({+-, the line is assumed to continue. This is generally correct but not always. For comparison, the analogous Python rule is that a line continues either with an explicit \ or when one of [({ has not yet been closed with its pair. The Python rule doesn't lead to any ambiguous cases that I am aware of, because unclosed parentheses would just be invalid syntax.
[f1, f2][intIndex]()
The 'solution' seems to be something like var functions = [f1, f2]
functions[intIndex]()
but that's a big loss for conciseness.Why would you write a return statement like that in the first place? (Not intended as flamebait - I am genuinely curious to know.) If it is a simple typo that separates the 'return' from the '[1,2,3]', then isn't the negligent typist just as likely to leave off the all-important semicolon? Or is there a genuine reason you might want to put the [1,2,3] on a line by itself?
Note that this is different to, say, Python, where newline explicitly indicates termination of a statement.
someFunction(parameterOne,
parameterTwo)
I think maybe the difference is that Python has always treated newlines the way it does now. JavaScript, on the other hand, has developed idioms based on the semicolon model. So has the rug has been snatched out from underneith its users by suddenly making them optional? Does the change make a lot of useful idioms harder to implement?function getObject()
{
return // returns undefined, not the object.
{
age: 30,
name: "axod"
}
}Personally I hate that style of bracketing, but some swear by it.
But since they are optional now, using them throughout our code won't help us for the example above: it would still wrongly return undefined because of the implied statement end. So it could be argued that semicolons provide a false sense of security in this new, semicolon-optional world. :^)
return
[1,2,3]
What should that do? Should it return, or return [1,2,3]? There are many other examples. Use semicolons and there is no debate over what should happen. Making them optional in javascript was a ridiculous idea.