Listing 7: Ăśberschreiben mit Merker-Variable
Anzeige
// Definition von "Person" wie in Listing 6
function Employee(firstName, lastName, department) {
Person.call(this, firstName, lastName)
var superGetName = this.getName;
this.getName = function() {
return superGetName.call(this) + " (" + department + ")";
};
}
var Temp = function() {};
Temp.prototype = Person.prototype;
Employee.prototype = new Temp();
Listing 8: Private Methoden und die self-Variable
function Person(firstName, lastName) {
var self = this;
self.getName = function() {
return firstName + " " + lastName;
};
var alertName = function() {
alert(self.getName());
};
self.showDebugInfo = function() {
alertName();
};
}
Listing 9: Protected
function Person(firstName, lastName) {
var prot = {};
var self = this;
prot.aProtectedMethod = function() {
alert("Protected method");
};
if (self._protCounter > 0) {
return prot;
}
}
function Employee(firstName, lastName, department) {
var self = this;
self._protCounter = (self._protCounter ? self._protCounter + 1 : 1);
var prot = Person.call(this, firstName, lastName);
self._protCounter--;
self.getName = function() {
prot.aProtectedMethod();
return firstName + " " + lastName;
};
if (self._protCounter > 0) {
return prot;
}
}
var Temp = function() {};
Temp.prototype = Person.prototype;
Employee.prototype = new Temp();
Listing 10: Namensräume/Pakete
if (!window.de) window.de = {};
if (!de.heise) de.heise = {};
if (!de.heise.ix) de.heise.ix = {};
if (!de.heise.ix.test) de.heise.ix.test = {};
de.heise.ix.test.Person = function(firstName, lastName) {
// Implementierung siehe vorheriges Listing
}
de.heise.ix.test.Employee = function (firstName, lastName, department) {
// Implementierung siehe vorheriges Listing
}
var Temp = function() {};
Temp.prototype = de.heise.ix.test.Person.prototype;
de.heise.ix.test.Employee.prototype = new Temp();
var max = new de.heise.ix.test.Employee("Max", "Muster", "Sales");
Listing 11: Dokumentations-Kommentar
/**
* Constructor for a Person object.
* @param {String} firstName the person's first name.
* @param {String} lastName the persons's last name.
*/
de.heise.ix.test.Person = function(firstName, lastName) {
// ...
}