ded / klass

a utility for creating expressive classes in JavaScript
751 stars 78 forks source link

Unable to hardcode super's constructor argument values from a subclass constructor #14

Closed ghost closed 11 years ago

ghost commented 12 years ago

Given a super class:

var View = klass(function(templateId, containerId) {
    this.templateId = templateId;
    this.containerId = containerId;
});

I can't find a way to set the values of templateId and containerId from within a subclass constructor and be able to use them from the superclass constructor:

var MenuView = View.extend(function() {
    this.supr('val1', 'val2'); // doesn't work, no supr available
});

var MenuView = View.extend(function() {
    this.templateId = 'val1'; // properly sets values, but not until after the superclass constructor executes
    this.containerId = 'val2';
});

Instead of being able to do initialization logic inside of View's constructor, I have to wait until after MenuView is instantiated, and then manually call my own initialize method after the constructor chain executes.

Jaben commented 11 years ago

Have this same problem. Any solutions?

ded commented 11 years ago

you have to do it like this:

var View = klass({
  initialize: function (a, b) {
    this.a = a
    this.b = b
  }
});

var MenuView = View.extend({
  initialize: function () {
    this.supr('val1', 'val2');
  }
});
ded commented 11 years ago

See "Object Literals" https://github.com/ded/klass#object-literals