Calling baseclass function in Javascript

Hi there,
How can I call the base class function testBaseFunction from the derived class, SaveCommand.
Thank you.

function Command(){
   this.testBaseFunction = function(value){
       alert(value);
     };
}
//In SaveCommand.js
function SaveCommand(){
      // call the base function here.
       this.testMe = function(){
             var value = 5;
             testBaseFunction(value);
        };
}
SaveCommand.prototype = new Command();
SaveCommand.prototype.constructor = SaveCommand;

var test = new SaveCommand();
test.testMe();

Open in new window

ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Do you mean this
function SaveCommand(){
      // call the base function here.
       this.testMe = function(){
             var value = 5;
             this.testBaseFunction(value);
        };
}

Open in new window

You have subclassed Command so you have access to the members of the superclass through this.
0
ambuliAuthor Commented:
Thank you Julian.  Actually, I realized that the real problem was that I had to call the base class function inside a callback.
Is there any restrictions on this?  How can I fix it?

function SaveCommand(){
       this.testMe = function(){
             var testArray = [2, 3, 4];
              testArray.forEach(function(entry){
                    this.testBaseFunction(entry);
              });
        };
}

Open in new window

0
Julian HansenCommented:
You need to create a local variable that refers to the object. The 'this' member changes depending on scope so is unreliable. Try this.

function SaveCommand(){
  var me = this;
  this.testMe = function(){
    var testArray = [2, 3, 4];
      testArray.forEach(function(entry){
        me.testBaseFunction(entry);
    });
  };
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.