Reputation: 2881
I'm trying to follow along the excellent tutorial backbonerails produced by Brian Mann. Unfortunately (for me because I'm a noob) he's using coffeescript. What's confusing is my assumptions of the following code:
class App.Views.Users extends Backbone.View
I believed was the equivalent of:
Users = Backbone.View.extend({});
in plain javascript. However, when I place the coffeescript code in trycoffeescript I get:
var _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
App.Views.Users = (function(_super) {
__extends(Users, _super);
function Users() {
_ref = Users.__super__.constructor.apply(this, arguments);
return _ref;
}
return Users;
})(Backbone.View);
My question is am I wrong in my assumption of what plain javascript should produce, am I incorrectly approaching the way to interpret the coffee script, or am I beyond hope?
Upvotes: 1
Views: 244
Reputation: 9559
Classes in coffeescript can't guarantee that the classes they're extending have an extend
method, so it wouldn't be possible for the coffeescript to compile to Backbone.View.extend
, without specifically requiring all the classes it's used with to provide the extend
method.
However, if you look at the source of _.extend (which Backbone uses) you'll see that it's fairly similar to the __extends
method that coffeescript generates and uses.
The coffeescript compiled version is obviously more verbose, but in practice I've never noticed a difference between class MyView extends Backbone.View
and MyView = Backbone.View.extends({}};
so you can probably use whichever one you prefer.
EDIT: One possible difference is actually the super
coffeescript keyword, which will only work if you're using coffeescript classes. However, you should still be able to replicate that functionality with .extends
by calling the superclass function directly.
Upvotes: 4