(function(b){})(a) equivalent
As far as I can see there's no obvious way to emulate this in CS:
(function(d) {
console.log(d.title);
})(document);
Would be cool to be able to, eg. with something like:
do (document as d) -> console.log d.title
Give it a try...
do (document) ->
console.log document.title
Well the main idea was to have a shorter name for the reference, hence the as d. Nevermind, I'll survive without it ;)
do ->
d = document
console.log d.title
Sure. Thanks for your attention.
Were you simply looking for ((d)-> console.log d.title)(document)?
@michaelficarra nope, I was looking for doing something like:
do (document as d, foobar as f, anotherone as a, yetanotherone as y) ->
console.log d, f, a, y
Instead of - as you suggest:
((d, f, h, a)->
console.log d, f, a, y
)(document, foobar, anotherone, yetanotherone)
This syntaxic sugar being only matter of readability and concision, nothing more, nothing less.
Not sure if it's a good idea, but you can shorten it that way.
do (document, window, anotherone, yetanotherone) ->
[d, w, a, y] = arguments
console.log d, w, a, y
@jblanche Thanks for the hint :) but I find using a dedicated keyword a bit more concise and elegant, still… matter of taste I presume :)
$ coco -bcs
let d = document
console.log d.title
(function(d){
console.log(d.title);
}.call(this, document));
I was looking for doing a loose augmentation module pattern in CoffeScript but I can not.
The pattern looks like this:
var MODULE = (function (my) {
// add capabilities...
return my;
}(MODULE || {}));
but in CoffeScript
MODULE = do (my) ->
my
generates
var MODULE;
MODULE = (function(my) {
return my;
})(my);
Support this syntax would be great!
Finally, I can use the following syntax, even if it is less readable than using the keyword do:
MODULE = ((my) ->
my
)(BAR or {})
Thx to @naholyr for the hint.
@piouPiouM: there's nothing stopping you
@michaelficarra arf, I made a typo ^^'
$ coffee -bep 'module = do (my = BAR or {}) -> my'
var module;
module = (function(my) {
return my;
})(BAR || {});
@n1k0:
$ coffee -bep 'do (d = document) -> console.log d.title'
(function(d) {
return console.log(d.title);
})(document);
@michaelficarra, I do not get the same result with version 1.2.0:
$ coffee --version
CoffeeScript version 1.2.0
$ coffee -bep 'module = do (my = BAR or {}) -> my'
var module;
module = (function(my) {
if (my == null) my = BAR || {};
return my;
})(my);
@michaelficarra @piouPiouM: Same here with 1.1.3 :/
$ coffee --version
CoffeeScript version 1.1.3
$ coffee -bep 'module = do (my = BAR or {}) -> my'
var module;
module = (function(my) {
if (my == null) my = BAR || {};
return my;
})(my);
$ coffee -bep 'do (d = document) -> console.log d.title'
(function(d) {
if (d == null) d = document;
return console.log(d.title);
})(d);
See Jeremy's comment and a477880edc9a46c4a47f8e1b6f2dcf6fc5fda6c7. Use master if you want that functionality or just wait for the next release.
@michaelficarra ahhh cool, that's awesome news :)