How to not use immediate functions in coffeescript

Asked 2 years ago, Updated 2 years ago, 90 views

When coffeescript is compiled, it is surrounded by immediate functions, but what should I do if I want to leave it out?

coffeescript

2022-09-30 18:05

2 Answers

Add the -b (--bare) option to the coffee command.

(However, the immediate function is to adjust the namespace so that variable names do not conflict with each other, so compiling in this way can cause unexpected problems.)

func.coffee:

square=(x)->x*x

cube=(x)->
    square(x)*x

coffee-c func.coffee output func.js:

//Generated by CoffeeScript 1.7.1
(function(){
  varcube, square;

  square=function(x){
    return x*x;
  };

  cube=function(x){
    return square(x)*x;
  };

}).call(this);

coffee-b-c func.coffee output func.js:

//Generated by CoffeeScript 1.7.1
varcube, square;

square=function(x){
  return x*x;
};

cube=function(x){
  return square(x)*x;
};


2022-09-30 18:05

If you want to be able to see functions and variables from other file sources when you run them in your browser,

foo.coffee:

window.foo=(x)->
  console.info"x=#{x}"

bar.js:

foo("test")

But I think you can refer to it


2022-09-30 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.