Superclass methods in javascript

I am writing some objects (classes that I think) in javascript. Class B inherits from class A. Class A has a method called isValid, and class B overrides this method. I am using the YUI extension feature for class B to 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 valid function
        // some more logic for class B.
        return booleanValue;
     }
});

      

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

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

      

Is something like this possible for methods? If not, what is a good practice for doing this? I am currently creating a helper method that is called inside the super class isValid method

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

      

I can then call the a_isValid function from class B. This works for me, but I would rather call the isValid function of the superclass directly if possible.

+2


source to share


2 answers


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 this work for you? 4th line should call Class1 (superclass) testMethod method.

+2


source


I am posting a different approach for documentation purposes.

If messageFormController

output from formController

, you call super.setView

like:



messageFormController.setView = function setView(element) {
    formController.setView.bind(this)(element);
    // Additional stuff
};

      

0


source







All Articles