Forum Discussion
dpeterson157
13 years agoContributor
Actually, that's not true. By accident I discovered that if you don't name the script files with the same name as the object being defined, the inheritance mechanism works perfectly, including the constructor functions. E.g.:
Script: mod_Person.sj:
function Person(fname) {
this.setFirstname(fname);
}
Person.prototype.setFirstname(fname) {
this.firstname = fname;
}
Person.prototype.toString = function() {
return "Person = '" + this.firstname + "'";
}
Script: mod_Employee.sj:
//USEUNIT mod_Person
Employee.prototype = new Person();
Employee.constructor = Employee();
Employee.superclass = Person.prototype;
function Employee(fname, id) {
this.setFirstname(fname);
this.setID(id);
}
Employee.prototype.setID = function(id) {
this.id = id;
}
Employee.prototype.toString() {
return Employee.superclass.toString + ", id = '" + this.id + "'";
}
I typed that from memory, so I hope I got it all right.
Script: mod_Person.sj:
function Person(fname) {
this.setFirstname(fname);
}
Person.prototype.setFirstname(fname) {
this.firstname = fname;
}
Person.prototype.toString = function() {
return "Person = '" + this.firstname + "'";
}
Script: mod_Employee.sj:
//USEUNIT mod_Person
Employee.prototype = new Person();
Employee.constructor = Employee();
Employee.superclass = Person.prototype;
function Employee(fname, id) {
this.setFirstname(fname);
this.setID(id);
}
Employee.prototype.setID = function(id) {
this.id = id;
}
Employee.prototype.toString() {
return Employee.superclass.toString + ", id = '" + this.id + "'";
}
I typed that from memory, so I hope I got it all right.
Related Content
- 4 years ago
- 12 years ago
- 6 years ago
- 3 years ago
Recent Discussions
- 16 hours ago
- 19 hours ago