Refining Superclass Method Calls in JavaScript

Comments

This is just a sample from the ASP.NET Ajax kit. However, it works for what its worth. It just registers the namespace, creates the class and registers it.
Type.registerNamespace("Demo");

Demo.Person = function(firstName, lastName, emailAddress) {
this._firstName = firstName;
this._lastName = lastName;
this._emailAddress = emailAddress;
}

Demo.Person.prototype = {

getFirstName: function() {
return this._firstName;
},

getLastName: function() {
return this._lastName;
},

getName: function() {
return this._firstName + ' ' + this._lastName;
},

dispose: function() {
alert('bye ' + this.getName());
}
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);You can extend the class via inheritance, like creating an employee.
Demo.Employee = function(firstName, lastName, emailAddress, team, title) {
Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]);

this._team = team;
this._title = title;
}

Demo.Employee.prototype = {

getTeam: function() {
return this._team;
},
setTeam: function(team) {
this._team = team;
},

getTitle: function() {
return this._title;
},
setTitle: function(title) {
this._title = title;
},
toString: function() {
return Demo.Employee.callBaseMethod(this, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();
}
}
Demo.Employee.registerClass('Demo.Employee', Demo.Person);

[this is good]
I wrote a four part "series" on how I've been doing this sort of thing with my Jade framework over on my Vox: psygnisfive.vox.com.

In summary: the process of inheritance creates an private object called supers, along with a public method called supers which will return that object (encapsulation, so you don't accidentally kill your supers object). The supers object contains all the methods of the super class, and, because in Jade, all variables are private, they are thus accessible to every method, so the super class methods have access to the objects private variables regardless of their scope. Also, the supers object contains the supers object of the class that it itself represents, so that if you have a long chain of inheritance, all super class methods right up the inheritance chain are available.
[this is good]

Post a comment

Already a Vox member? Sign in

ydnar

About Me

ydnar
One day we’ll merge our social graphs and be friends forever
Dopplr:
ydnar
FFFFOUND!:
ydnar
Flickr:
shaderlab
Last.fm:
shaderlab
Other:
http://is.ydnar.in/
Other:
http://ydnar.com/
SonicLiving:
6903
Twitter:
shaderlab

Neighborhood

Explore friends, family, friends & family, or entire neighborhood.

Archives

Favorites + last.fm