views:

77

answers:

1

Consider the following code:

var Widget = new Class({
       Implements: [Options],
       options: {
          "name" : "BaseWidget"
       },
       initialize: function(options) {
          alert("Options are: " + JSON.stringify(options)); //alerts "Options are: undefined"
          this.setOptions(options);
          alert("My options are: " + JSON.stringify(this.options)); //alerts "My options are: { 'name' : 'BaseWidget' }"
       },
       getName: function() {
          return this.options.name;   
       }
});

var LayoutWidget = Widget.extend({    
       initialize: function() {
          this.parent({ "name" : "Layout" });
       }
});

alert(new LayoutWidget().getName()); //alerts "BaseWidget"

I am having difficulty in determining why the argument passed in the "this.parent()" call in "initialize" function of LayoutWidget is coming through as "undefined" in the initialize function of Widget.

I am using MooTools 1.2.2. Would somebody be able to point me in the right direction?

+1  A: 

check this: http://www.jsfiddle.net/F4hTS/

slight difference in form.

var Widget = new Class({
    Implements: [Options],
    options: {
        "name" : "BaseWidget"
    },
    initialize: function(options) {
        alert("Options are: " + JSON.stringify(options)); //alerts "Options are: undefined"
        this.setOptions(options);
        alert("My options are: " + JSON.stringify(this.options)); //alerts "My options are: { 'name' : 'BaseWidget' }"
    },
    getName: function() {
        return this.options.name;   
    }
});

Widget.LayoutWidget = new Class({   
    Extends: Widget,
    initialize: function(options) {
        this.parent(options);
    }
});

alert(new Widget.LayoutWidget({ "name" : "Layout" }).getName()); //alerts "Layout"
Dimitar Christoff
interesting, all the examples I saw used Class.extend(). this works though, thanks
Erin Drummond