Вопрос

Is it somehow possible to pass some arguments into the coffeescript-class iife?

A CoffeeScript class-instance like this:

class App

results normally in this:

App = (function() {

  function App() {}

  return App;

})();

However, maybe there's a way to populate the anonymous-wrapper with some arguments, like this:

App = (function($) {

  function App() {}

  return App;

})(jQuery);

Currently I wrap each module in an superfluous extra wrapper:

(($) ->
  class App
)(jQuery)

But I find this definitely not beautiful (like coffescript usually is).

Это было полезно?

Решение

I think elclanrs's comment is the answer you are looking for.

In coffeescript do ($) -> will compile to (function($){ … })($).

But do ($ = jQuery) -> will compile to (function($){ … })(jQuery).

There really isn't any cleaner way of doing it than this. It's one line at the top of the file and makes it very clear what you are doing.

do ($ = jQuery) ->
  class App

Другие советы

You could do something like this

class A extends GenericClass("some_parameter")

where GenericClass is defined as

GenericClass = (params) ->
    return { some: objects }

Honestly I wouldnt do that as it totally makes it intransparent what you try to do. Maybe you can explain what you try to do in terms of features/business value and not so much in terms of CoffeeScript syntax?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top