0

I am trying to make a parent data access layer class that is inherited by multiple classes.

parent class:

var DataAccess = function() {
   this.Save = function(){
        alert(this.ListName);  //works
        SaveLogic(this.Id);    //doesnt work
}
}

Child Class:

var Job = function(){
  Job.prototype.ListName = 'MyList';  //works

  this.Save = function(){
  Job.prototype.Save().call(this);
  //specific Job Save logic
  }
}

Job.prototype = new DataAccess();

Now in my main class:

var aJob = new Job();
aJob.Id = 1;
aJob.Save();  //Does not work. Prototype can not see aJob.Id..

As you can see, I need to create a parent function with shared variables such as ID, so when I inherit the parent class, I can assign values to these variables so the shared logic of hte parents class work, then my extended class's can have specific logic

Michael
  • 8,229
  • 20
  • 61
  • 113
  • 2
    Never add methods to the prototype inside a constructor. – Adam Jenkins Jun 04 '14 at 11:38
  • you code snippet is either incomplete or you forgot to make the Job prototype inherit from DataAcess.Either way your snippet shows you have little understanding of how prototypal inheritance works in javascript. – mpm Jun 04 '14 at 11:55
  • I forgot to add a line in the snippet. – Michael Jun 04 '14 at 12:04
  • Omit the parenthesis in `Job.prototype.Save().call`. – Bergi Jun 04 '14 at 13:22
  • What is `SaveLogic(this.Id)` supposed to do? Does it not work because `SaveLogic` is defined nowhere? Or what does not work? – Bergi Jun 04 '14 at 13:25

3 Answers3

0

You can start with construction like this:

var DataAccess = function() {
    this.Save = function(){
        console.log('DataAccess Save call', this.ListName, this.Id);
    }
}

var Job = function(){
  this.ListName = 'MyList';
}
Job.prototype = new DataAccess();
/**
 * Delete me to use parent's Save method.
 */
Job.prototype.Save = function(){
    console.log('Job Save call', this.ListName, this.Id);
}

var aJob = new Job();
aJob.Id = 1;
aJob.Save();

@stivlo described how it works in his answer here: https://stackoverflow.com/a/4778408/1127848

Community
  • 1
  • 1
Pawel Uchida-Psztyc
  • 3,735
  • 2
  • 20
  • 41
0

The problem I had was I wanted to reuse the same code. I think I have worked it out this way, im still not 100% its the right way to go with prototype programming :

function DataAccess() {
   //setup common variables
}

DataAccess._Save_(listname, id){
   commonSaveLogic(id);
   doStuff(listname);
}

function Job() {
    this.ListName = 'Jobs';
    DataAccess.call(this); //call DataAccess Constructor
}

Job.prototype = DataAccess;
Job.prototype.constructor = Job;

Job.ProtoType.Save = function(){
   this._Save_(this.ListName, this.Id);
}


function AotherList() {
      this.ListName = 'AnotherList';
      DataAccess.call(this);
}
//same as above. Job and Another list both inherit off DataAccess. 
Michael
  • 8,229
  • 20
  • 61
  • 113
0

Dont use .prototype inside the constructor. We define .prototype for sharing same copy to all objects.

You are missing here many things. I'm explaining one by one:

First : SaveLogic(this.Id); //doesnt work
Because You don't use this with the function so it's a global function not a constructor function. And you don't have defined it any where so there will be an error like function SaveLogic not defined
To prevent this error, define the function somewhere.

Second : You have passed this.Id as a parameter. Id using the line aJob.Id = 1; will not be accessible within the SaveLogic(this.Id); because Id is a property of aJob not of ajob.prototype. this.ListName will be available here because it's a property of prototype. So it you want to get Id inside SaveLogic() function, define it as prototype property.

Third : when this line aJob.Save(); will be invoke it will call

this.Save = function(){
  Job.prototype.Save().call(this);
  //specific Job Save logic
}

Job.prototype.Save() will search for a function named as Save(). Which is not defined in Job's prototype so function not defined error will occur.

Fourth : call() can not be called anyhow excepts either DataAccess.call() or Job.call();
call() is just like the constructor call excepts it's first parameter get assigned to the constructor's this object.
Here i have improved your code. Just copy and paste it in your editor and see what is going here.

Try this :

function SaveLogic(Id)
{
    alert(Id);
}
var DataAccess = function() {
   this.Save = function(){
        alert(this.ListName);  //works
        SaveLogic(this.Id);
        return this;        //doesnt work
    }
    this.call = function() {
        alert('call is called here');
    }
}

var Job = function(){
  Job.prototype.ListName = 'MyList';  //works

  this.Save = function(){
    //console.log(Job.prototype.Save());
    Job.prototype.Save().call(this);
    //specific Job Save logic
  }
}
Job.prototype = new DataAccess();

var aJob = new Job();
Job.prototype.Id = 1;
aJob.Save();  //Does not work. Prototype can not see aJob.Id..
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
intekhab
  • 1,566
  • 1
  • 13
  • 19
  • It's showing only that Job is inheriting the DataAccess property. I thing nothing is wrong with this. – intekhab Jun 04 '14 at 13:29