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.