Closed qqueue closed 12 years ago
I have to agree that a cascade expression should evaluates to the cascadee value. While maximally minimal, the current syntax seems to miss the major use cases by limiting itself to the top-level.
We add no new keywords as a policy, so we may have to introduce some new symbol operator. Say, +>
:
something-with new Thing +>
&foo = 'bar'
&baz = quux 'stuff'
And/or we can make with
return the target by default:
something-with with new Thing
@foo = 'bar'
@baz = quux 'stuff'
Hmm, is it even worth keeping with
's current behavior as sugar for .call(thing)
? I can't imagine many use cases where having the actual IEFE would be preferrable over the more performant cascade. I mean, with
's example in the docs could be completely replaced with cascade. If one actually needs a new scope, let
makes more sense than with
anyway.
Using with
as the cascade-that-evaluates-to-cascadee operator avoids using creating any more reserved words, and it "reads" better than +>
, IMO.
For the times that you do need to set the context for an IEFE, you could just special-case let
to detect constructs like let @ = something
or let this = something
to set the context appropriately.
Great point.
The purpose of with
was exactly the cascade-ish construct. Now that we added an actual cascade, we should reconsider its raison d'etre.
So the proposal becomes:
Change with
to an expression version of cascade.
head.appendChild do
with document.createElement \title
&textContent = btitle
↓
var x$;
head.appendChild((x$ = document.createElement('title'), x$.textContent = btitle, x$))
Make let
support this
-substitution again.
let this = foo, bar = baz
...
# as:
((bar) ->
...
)call foo, baz
That looks fine to me, though the readability of the compiled js suffers a bit with the comma expression. Similar to #115, it'd be nicer if it compiled to
head.appendChild(
(x$ = document.createElement('title')
, x$.textContent = btitle
, x$))
Just a minor issue.
I've got a fair amount of code that looks like this:
or more purely, like this:
where some of the code initializes and mutates an object just to pass it into another function (or return it). When available, I usually use a full constructor:
But some constructors, e.g. DOM
Image
, have stupid arguments (nosrc
), so an extra line of initialization is necessary.I know I can use
new
with a block to create thenew function() {...}
form, but that doesn't work withImage
, and it introduces overhead. Thus, my straw man is:Constructor Cascade
Similar to cascade, but expression returns the original reference (after execution of the block) instead of the result of the last expression in the block.
=>
Advantages
return this
at the end of a function, e.g. when usingwith new XMLHttpRequest
$(body).append($('<p>').text('just look at all this overhead!'))
Problems
construct