Trouble with inheritance

I’m having some trouble setting up classical inheritance and its not working, wondering what I’m doing wrong.

Please consider the following code


var gn = new GroupNoteOn(5, "C4");

console.log(gn.id);
console.log(gn.nn);

if(gn instanceof GroupNoteOn) {
    console.log("Instance of GroupNoteOn");
}

if(gn instanceof GroupSwitch) {
    console.log("instance of GroupSwitch");
}

if(gn instanceof KeySwitch) {
    console.log("instance of Key Switch");
}

gn.test();
gn.testKey();

//====================
// KeySwitch class
//====================

function KeySwitch() {};
KeySwitch.prototype.testKey = function() {
    console.log("keyswitch testKey");
};

//====================
// GroupSwitch class
//====================

function GroupSwitch(id) {
   KeySwitch.call(this);
   this.id = id;
};
GroupSwitch.prototype = Object.create(KeySwitch.prototype);
GroupSwitch.prototype.constructor = GroupSwitch;

GroupSwitch.prototype.test = function() {
    console.log("GroupSwitch test");
};

//=======================
// GroupNoteOn class
//=======================

function GroupNoteOn(id, nname) {
   GroupSwitch.call(this,  id);
   this.nn = 45;
};
GroupNoteOn.prototype = Object.create(GroupSwitch.prototype);
GroupNoteOn.prototype.constructor = GroupNoteOn;

Basically it produces an error when trying to call gn.test(). Also the instanceof functions are not returning true for KeySwitch or GroupSwitch.

What am I doing wrong in setting up the prototype inheritance chain?

When gn.test() is called, the test method doesn’t yet exist on that object.

Edit:

Oh, you edited the code in your OP to something different

With the code currently in the OP, the browser console says that ObjectCreate doesn’t exist.

Normally Object.create is used instead, or you might want to supply a separate function for ObjectCreate if that exists.

Interesting. so functions are scanned through the js file all the way through before exeuction, assigning them to prototypes aren’t. (sigh).

I wonder if there is any way I can have the declaration of class methods at the end of the js file, for this application, I need to be able to do it that way…

you spotted some code typos in my original post but they have been corrected. Still wondering if there is any way to have my classical class inheritance somehow defined at the end of a js file, so that the globally executed code will be at the top of the file.

There isn’t. You must define the function method first before you use it.
Attempting to use a non-existant function method results in the problems that you currently face.

For anyone searching later, I think I kind of figured out a way

SETUP_OOP();

var gn = new GroupNoteOn(5, "C4");

console.log(gn.id);
console.log(gn.nn);

if(gn instanceof GroupNoteOn) {
    console.log("Instance of GroupNoteOn");
}

if(gn instanceof GroupSwitch) {
    console.log("instance of GroupSwitch");
}

if(gn instanceof KeySwitch) {
    console.log("instance of Key Switch");
}

gn.test();
gn.testKey();

//====================
// KeySwitch class
//====================

function KeySwitch() {
    this.testKey = function() {
        console.log("keyswitch testKey");
    }
};

//====================
// GroupSwitch class
//====================

function GroupSwitch(id) {
   KeySwitch.call(this);
   this.id = id;

   this.test = function() {
        console.log("GroupSwitch test");
   }
};

//=======================
// GroupNoteOn class
//=======================

function GroupNoteOn(id, nname) {
   GroupSwitch.call(this,  id);
   this.nn = 45;
};

function SETUP_OOP() {
    GroupSwitch.prototype = Object.create(KeySwitch.prototype);
    GroupSwitch.prototype.constructor = GroupSwitch;
    GroupNoteOn.prototype = Object.create(GroupSwitch.prototype);
    GroupNoteOn.prototype.constructor = GroupNoteOn;
}

The above seems to work. I think this is because javascript scans for all functions before exeucting the global level. So each of the constructor functions is defined before starting. It can then run SETUP_OOP() first to finalize the prototype chain before the rest of the global code execution.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.