Created
December 1, 2011 08:24
-
-
Save dherman/1414956 to your computer and use it in GitHub Desktop.
Monocle-mustache using semicolons (intuition: blocks and statements, not object literals)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme | |
// and by https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142 | |
// traditional method chaining with combinators: | |
console.log(range(1, 3) | |
.concat(range(4, 6)) | |
.map(function(x) { return x * x }) | |
.filter(function(x) { return x % 2 === 0 }) | |
.reverse()); | |
// method chaining with cascades: | |
array.{ | |
pop(); | |
pop(); | |
pop(); | |
}; | |
path.{ | |
moveTo(10, 10); | |
stroke("red"); | |
fill("blue"); | |
ellipse(50, 50); | |
}; | |
// method chaining with cascades and ASI: | |
array.{ | |
pop() | |
pop() | |
pop() | |
}; | |
path.{ | |
moveTo(10, 10) | |
stroke("red") | |
fill("blue") | |
ellipse(50, 50) | |
}; | |
// object initialization with cascades: | |
this.{ | |
foo = 12; | |
bar = "hello"; | |
mumble = false; | |
}; | |
// Bob's compound example: | |
document.query('#myTable').{ | |
queryAll('.firstColumn').{ | |
style.{ | |
background = 'red'; | |
border = '2px solid black'; | |
}; | |
text = 'first column'; | |
}; | |
queryAll('.lastColumn').{ | |
style.background = 'blue'; | |
text = 'last column'; | |
}; | |
}; |
I don't fully understand the difference between this syntax and the with
statement.
Can you enlighten me please?
You don't have a dynamic scope this way.
bar = 42
}```
bar will always be set on foo. In a with statement, bar might also be a global variable or a variable in a function scope...
I see.
Wouldn't it be simpler if we redefined with
to work this way?
To be honest, I had with in VB back in the day and it wasn't a problem. The difference between JS and VB with was that VB required a dot at the start. This actually disambiguated everything pretty effectively.
Although I'm not proposing we re-invent it, either...
I'd use [ instead of {.
@JulianBirch I've got to admit, one issue with with is how hard it is to talk about it.
Well, it isn't as hard as to talk about this...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. I assume that only property assignments (including recursive cascades) and method calls are allowed inside such blocks and that the rhs of assignments are never interpreted relative to the block qualifier(?)