2

I am writing some objects (classes I guess) in javascript. Class B inherits from Class A. Class A has a method called isValid, and class B overrides that method. I am using the YUI extend function to have Class B extend Class A.

A = function(){
}
A.prototype = {
   isValid:function(){
       /* Some logic */
       return booleanValue;
   }
}

B = function(){
}

YAHOO.lang.extend(B, A,{
     isValid:function(){
        // call class A's valid function
        // some more logic for class B.
        return booleanValue;
     }
});

What I want to be able to do is call Class A's isValid function inside class B's isValid function. The question is, can I access class A's isValid method from class B's isValid method? I know that you can access class A's constructor from inside Class B's constructor with the following line

this.constructor.superclass.constructor.call(this,someParam);

Is something similar possible for methods? If not, what is a good practices for doing this? Currently I am making a helper method called inside the super class' isValid method

A.prototype = {
    a_isValid:function(){
       // class A's is valid logic
       return booelanValue;
    },
    isValid:function() {return this.a_isValid();}
}

Then I can call the a_isValid function from class B. This does work for me but I would prefer to call the super class' isValid function directly if possible.

cavila
  • 7,834
  • 5
  • 21
  • 19
Zoidberg
  • 10,137
  • 2
  • 31
  • 53

2 Answers2

2

From YUI docs:

YAHOO.lang.extend(YAHOO.test.Class2, YAHOO.test.Class1); 
YAHOO.test.Class2.prototype.testMethod = function(info) { 
// chain the method 
YAHOO.test.Class2.superclass.testMethod.call(this, info); 
alert("Class2: " + info); 
}; 

Doesn't it work for you? The 4th line should call Class1's (superclass) testMethod.

wtaniguchi
  • 1,324
  • 14
  • 22
0

I am posting another approach for documentation purposes.

If messageFormController derives of formController, you call super.setView as:

messageFormController.setView = function setView(element) {
    formController.setView.bind(this)(element);
    // Additional stuff
};
Freeman
  • 5,810
  • 3
  • 47
  • 48