5
Sep
2006

"Proper" OO Inheritance for JavaScript

... this is the subject of a post by juerg leny to the helma mailing list.

Juerg and Hannes attemted (independendly) to improve an approach by Dean Edwards.

Dean meantions a few goals:
  • I want to easily create classes without the MyClass.prototype cruft
  • I want method overriding with intuitive access to the overridden method (like Java’s super)
  • I want to avoid calling a class’ constructor function during the prototyping phase
  • I want to easily add static (class) properties and methods
And proposes the following syntax:
A second parameter passed to the extend method of a class defines the class interface

var MyClass = Base.extend({
  foo: "value",
  someFunction: function() { }
}, {
  PI: 3.14
})
Having extendable "classes" and interfaces makes it a lot easier to write reuseable and extendable code in Javascript. Which is a must for Helma(2).

But i don't like the syntax too much. The main problem i have is that reading the example above it's hard to figure out which of the two arguments is the static hash and which one the class methods. And in most languages static properties are declared at the beginning of a class, because in most cases they will be used in the class methods.

Something like this would be more readable:

var MyClass = Base.extend({
   ONE: 'One',
   $methods: {
     one: function() { }
   }
});
In this example all properties of the hash (which is the only argument) are becoming static properties of the "class" except the children of hash "$methods". "$methods" could also be called "prototype" which is the standard naming in Javascript.

Some more links:
more Dean Edwards
Sam does like Deans approach
The $class library
$class example
blog comments powered by Disqus
logo

Mat's Blog

California Dreaming

Users Status

You are not logged in.

Search

 

Get Firefox

Get Firefox!

Currently Reading


William Gibson, Jarreth Merz, Matthias Scherwenikas
Neuromancer, 3 Audio-CDs

Archive

September 2006
Sun
Mon
Tue
Wed
Thu
Fri
Sat
 
 
 
 
 
 1 
 2 
 3 
 4 
 6 
 7 
 9 
10
11
12
13
14
16
17
18
19
20
21
23
24
25
26
27
29
30
 

Recent Updates

und wie sieht es mit...
und wie sieht es mit deiner privaten Hochzeit aus? ;-) wünsche...
flog - 23. Oct, 20:49
Danke!
Danke Matthias für deinen Beitrag zu diesem Schritt....
Sierra - 22. Oct, 12:44
Ja, das Offline Problem...
Ja, das Offline Problem ist auch mein Hauptproblem....
matthias - 23. Sep, 14:36
ich verwende am Mac ausschließlich...
ich verwende am Mac ausschließlich die Browser...
smeidu - 23. Sep, 14:22
Stimmt -
Den Eindruck kann ich nur bestätigen; in der Bedienung...
N_Haase - 29. Jul, 13:24
Dach
und da oben am Dach, da sollte doch seit Jahren eine...
Sierra - 12. Jun, 10:47
but yammer accepts
no Austrian mobile numbers :(
cqeb - 15. Dec, 23:45
das verpasse ich leider......
das verpasse ich leider... http://twitter.com/flog/st atus/998712029
flog - 10. Nov, 21:38

Status

Online for 4608 days
Last update: 3. Mar, 15:52

Credits

vi knallgrau GmbH

powered by Antville powered by Helma


Creative Commons License

xml version of this page

twoday.net AGB