tags:

views:

632

answers:

1

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.

+1  A: 

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
I will try that, thanks!
Zoidberg
Worked great... and makes soo much sense, I should have realized it based on the constructor method. Thanks!
Zoidberg