3

with LiveScript, when you write some code in a file, the default compiler wraps the compiled code in an anonymous function :

Compiling this:

add10 = -> it + 10

gives that:

(function(){
  var add10;
  add10 = function(it){
    return it + 10;
  };
}).call(this);

And that is ok ! but i want to export some API functions for my module, so what i do now is this :

add10 = -> it + 10

this.add10 = add10

giving that :

(function(){
  var add10;
  add10 = function(it){
    return it + 10;
  };
  this.add10 = add10;
}).call(this);

I work in a browser context, and i wonder if there were no better solution. It actually works but i would rahter something like this:

(function(w){
  var add10;
  add10 = function(it){
    return it + 10;
  };
  w.add10 = add10;
}(this));

It's less code (no big difference in such a small example).

lud
  • 1,941
  • 20
  • 35
  • What's wrong with just adding a var w = this; before? – ming_codes Jun 15 '13 at 16:46
  • I don't know i'm looking for good practices tips ! – lud Jun 18 '13 at 15:40
  • If I understand correctly, you prefer the last example over the second to last because it's less code. But that doesn't matter if you use compression -- which you should, especially if the file is big enough that the difference between `w.` and `this.` becomes significant. – Peter-Paul van Gemerden Jul 14 '13 at 10:35
  • yeah i use compression. The real reason is that i'd rather avoid using of the this keyword – lud Jul 14 '13 at 18:16

1 Answers1

4

I'd say, use :

export add10 = (+ 10)

:).

Ven
  • 19,015
  • 2
  • 41
  • 61
  • 3
    the home page of Livescript does not mention this feature. I've already tried to search for 'export' on it but i didn't tried in code. I should have :) – lud Jun 24 '13 at 08:44
  • I opened an issue for that already : https://github.com/gkz/LiveScript/issues/311 – Ven Jun 24 '13 at 11:03