Javascript calling derived class function from base class

Hi there,
Is this possible?  I want to call gatherData() from the base class..
Thank you.

function Command(){
     
       this.gatherData = function() {
                console.lot("Not implemented");
       }
       
    this.generateReport = function() {
             console.log("generateReport implemented in Command");
             this.gatherData(); <---- this should call the derived class version.  Is it possible?
    }
}

//In SaveCommand.js
function SaveCommand(){

       this.gatherData = function() {
              console.log("here it is implemented");
       }
       
       this.testMe = function() {
              this.generateReport();
        }
}

SaveCommand.prototype = new Command();
SaveCommand.prototype.constructor = SaveCommand;

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

Open in new window

ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Alexandre SimõesManager / Technology SpecialistCommented:
I've tested your code here: http://jsfiddle.net/AlexCode/pnx2zwby/ and it works as you want, so what exactly is your question? :)
ambuliAuthor Commented:
Thank you.  I think I again missed the callback in the code.  I am getting this.gatherData is not a function when I do as below.

function Command(){
     
       this.gatherData = function() {
                console.lot("Not implemented");
       }
       
    this.generateReport = function() {
             console.log("generateReport implemented in Command");
             var arr = [10, 20, 30];
              arr.forEach(function(entry){
                     this.gatherData(); <---- this should call the derived class version.  Is it possible?
               });
    }
}

//In SaveCommand.js
function SaveCommand(){

       this.gatherData = function() {
              console.log("here it is implemented");
       }
       
       this.testMe = function() {
              this.generateReport();
        }
}

SaveCommand.prototype = new Command();
SaveCommand.prototype.constructor = SaveCommand;

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

Open in new window

ambuliAuthor Commented:
So, when I pass in the derived object's this to the base class, it is working as expected.  But, is there any other way?  Will this approach give any problems?
Thank you.

 function Command(){
        var _context = this;

        this.gatherData = function() {
            alert("Not implemented");
        }

        this.generateReport = function(derivedContext) {
            console.log("generateReport implemented in Command");
            var arr = [10, 20, 30];
            arr.forEach(function(entry){
                derivedContext.gatherData();
            });
        }
    }

    //In SaveCommand.js
    function SaveCommand(){
        var _derivedContext = this;

        this.gatherData = function() {
           alert("here it is implemented");
        }


        this.testMe = function() {
            this.generateReport(_derivedContext);
        }
    }

    SaveCommand.prototype = new Command();
    SaveCommand.prototype.constructor = SaveCommand;

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

Open in new window

zzynxSr. Software engineerCommented:
But, is there any other way?
There is. Try this adapted code for your generateReport() method:

    this.generateReport = function() {
        console.log("generateReport implemented in Command");
        var arr = [10, 20, 30];
        var me = this;
        arr.forEach(function(entry){
            me.gatherData();
        });
    };

Open in new window


If you write 'this' inside the forEach-loop, it represents the context of the (loop)function.
You want the context of your (Save)Command object.
So, save it before you start the loop in a variable (typically named 'me') and use that variable inside the loop.

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
zzynxSr. Software engineerCommented:
Thanx 4 axxepting
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.