0

as defined here:

Module.init is implemented like:

Module.init = Controller.init = Model.init = (a1, a2, a3, a4, a5) ->
  new this(a1, a2, a3, a4, a5)

why is it like this? why define 5 attributes and not use attrs... so attributes are not fixed to 5....

new this(attrs...)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
aschmid00
  • 7,038
  • 2
  • 47
  • 66

2 Answers2

2

Maybe it's because the compiled JS is much smaller (Spine.js makes a lot of emphasis on low footprint).

Module.init = Controller.init = Model.init = (a1, a2, a3, a4, a5) ->
  new this(a1, a2, a3, a4, a5)

Compiles to:

Module.init = Controller.init = Model.init = function(a1, a2, a3, a4, a5) {
  return new this(a1, a2, a3, a4, a5);
};

While:

Module.init = Controller.init = Model.init = (args...) ->
  new this args...

Compiles to the much more convoluted:

var __slice = [].slice;

Module.init = Controller.init = Model.init = function() {
  var args;
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  return (function(func, args, ctor) {
    ctor.prototype = func.prototype;
    var child = new ctor, result = func.apply(child, args), t = typeof result;
    return t == "object" || t == "function" ? result || child : child;
  })(this, args, function(){});
};

This is because in JavaScript the new operator and apply cannot be used in conjunction :(

epidemian
  • 18,817
  • 3
  • 62
  • 71
  • sure but its a limit. and limits are not cool specially implemented in a library/framework you are supposed to subclass and modify to your needs. – aschmid00 May 17 '12 at 15:31
  • @aschmid00 Yeah, that's true. But this is pure speculation on my part really; there probably is another reason. I'd probably be a good idea to ask Alex MacCaw, creator of Spine, himself instead =P – epidemian May 17 '12 at 16:47
  • i got the answer from the author: https://groups.google.com/forum/#!topic/spinejs/ao3m_ucxiXo – aschmid00 May 18 '12 at 14:19
  • @aschmid00 Cool! I like that he's getting rid of this function in the next version though =P – epidemian May 18 '12 at 15:42
1

Either someone didn't know about splats, or they felt that it was cleaner to do it this way, or maybe they were thought it would be more performant to not use a bunch of extra logic to process the arguments object.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • i understand but this is limiting what you can actually do besides that if you want to loop over attributes you get a few undefined and im not sure if you could even pass more than 5 attributes to the init function. – aschmid00 May 17 '12 at 15:30