coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

(function(b){})(a) equivalent

Open n1k0 opened this issue 14 years ago • 19 comments

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

n1k0 avatar Dec 12 '11 22:12 n1k0

Give it a try...

do (document) ->
  console.log document.title

jashkenas avatar Dec 12 '11 22:12 jashkenas

Well the main idea was to have a shorter name for the reference, hence the as d. Nevermind, I'll survive without it ;)

n1k0 avatar Dec 12 '11 22:12 n1k0

do ->
  d = document
  console.log d.title

jashkenas avatar Dec 12 '11 22:12 jashkenas

Sure. Thanks for your attention.

n1k0 avatar Dec 12 '11 22:12 n1k0

Were you simply looking for ((d)-> console.log d.title)(document)?

michaelficarra avatar Dec 12 '11 23:12 michaelficarra

@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.

n1k0 avatar Dec 13 '11 08:12 n1k0

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 avatar Dec 13 '11 09:12 jblanche

@jblanche Thanks for the hint :) but I find using a dedicated keyword a bit more concise and elegant, still… matter of taste I presume :)

n1k0 avatar Dec 13 '11 09:12 n1k0

$ coco -bcs
  let d = document
    console.log d.title

(function(d){
  console.log(d.title);
}.call(this, document));

satyr avatar Dec 13 '11 15:12 satyr

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!

piouPiouM avatar Dec 19 '11 15:12 piouPiouM

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 avatar Dec 19 '11 16:12 piouPiouM

@piouPiouM: there's nothing stopping you

michaelficarra avatar Dec 19 '11 16:12 michaelficarra

@michaelficarra arf, I made a typo ^^'

piouPiouM avatar Dec 19 '11 17:12 piouPiouM

$ coffee -bep 'module = do (my = BAR or {}) -> my'
var module;

module = (function(my) {
  return my;
})(BAR || {});

michaelficarra avatar Dec 19 '11 17:12 michaelficarra

@n1k0:

$ coffee -bep 'do (d = document) -> console.log d.title'
(function(d) {
  return console.log(d.title);
})(document);

michaelficarra avatar Dec 19 '11 17:12 michaelficarra

@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);

piouPiouM avatar Dec 19 '11 17:12 piouPiouM

@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);

n1k0 avatar Dec 19 '11 17:12 n1k0

See Jeremy's comment and a477880edc9a46c4a47f8e1b6f2dcf6fc5fda6c7. Use master if you want that functionality or just wait for the next release.

michaelficarra avatar Dec 19 '11 17:12 michaelficarra

@michaelficarra ahhh cool, that's awesome news :)

n1k0 avatar Dec 19 '11 17:12 n1k0