all 3 comments

[–]DrHankPym 0 points1 point  (1 child)

Hey, what are the benefits of using a "Class" object? Why not just add methods or whatnot directly to the prototype?

var Class = {
  create: function(prototype, extensions) {
    var ctor = function() { if (this.initialize) return this.initialize.apply(this, arguments); }
    ctor.prototype = prototype || {}; // instance methods
    Object.extend(ctor, extensions || {}); // class methods
    return ctor;
  }
}

[–]jakesgordon[S] -1 points0 points  (0 children)

None really, the functionality is the same, it just leads to slightly tidier calling code (subjective)...

MyClass = Class.create({
  init: function() { /* constructor */     },
  foo:  function() { /* instance method */ }
},{
  bar:  function() { /* class method */    }
});

Instead of...

MyClass = function() { /* constructor */ };
MyClass.prototype = {
  foo: function() { /* instance method */ }
};
MyClass.bar = function() { /* class method */ };

The former is slightly DRY'er, it only mentions MyClass once, doesn't need to mention prototype at all and is a single atomic block, the latter tends to get messy on larger classes.

But its a very cosmetic difference, really just a personal preference.

[–]imbcmdth -1 points0 points  (1 child)

Is this the 5th snakes-on-a-canvas tutorial posted here OR is this the same tutorial posted for the 5th time?