Helma Logo
main list history

Version 26 by Lehni on 20. October 2006, 12:13

19Classes are defined by using Object.extend. The function takes two hash-lists a hash-list (JS Objects), defining the first one for the instance fields,. the Static fields are defined by adding a second hash-list to the first one for static class fields. The static list is optionalunder the property $static. Inheritance is implemented for both, so static class methods can be inherited from base classes and called through $super too. Each class that is created in such a way recieved the static functions "extend" and "inject". extend does the same as Object.extend, .inject works similarly and has the same arguments, but instead of creating a new class, it simply adds the fields to the existing class, so modification of classes at runtime is possible. If a function is overriden in such a way, $super can be used to call the previous definition of that function before the call to inject.
23Note that the constructor function's name was changed to "initialize", just like in Prototype$constructor, because "constructor" causes an error on some browsers (e.g. Mac IE).
116With a simple addition, this code should become again almost compatible with Ben's prototype branch,. The only change needed is the renaming of the constructors from initialize to test it out:$constructor (or change that part of extend.js to look for initialize instead).

Version 25 by Lehni on 20. October 2006, 12:08

27 var ClassOne = Object.extend({ // instance fieldsextend({
28 // instance fields
29 $constructor: function() {
30 writeln("Creating ClassOne instance");
31 },
30 }
31 }, { // class fields
32 test: function() {
33 write('Static Hello from ClassOne');
32  $static:
33    // define a constantclass fields
34  PI: Math.PI test: function()
35 write('Static Hello from ClassOne');
36 },
37 // define a constant
38 PI: Math.PI
39 }
36 $constructor: function() {
37 writeln("Creating ClassTwo instance");
38 },
40 },
41 $static: {
42 test: function() {
43 this.$super();
44 writeln(' - Static Hello from ClassTwo');
45 }
42 }, {
43 test: function() {
44 this.$super();
45 writeln(' - Static Hello from ClassTwo');
46 }
45 writeln();
46 writeln("// var obj = new ClassTwo();");
58 writeln("// again with modified function in base class after inheritance");
59 after inheritance");
73 writeln("// again with modified static function in base class after inheritance");
74 after inheritance");
78 writeln("TwoClassTwo.Pi PI = ", ClassTwo.PI);
79 

Version 24 by Lehni on 05. September 2006, 10:13

88Output:
89
90 // obj.test();
91 Hello from ClassOne - Hello from ClassTwo
92 
93 // again with modified function in base class after inheritance
94 Hallo aus KlasseEins - Hello from ClassTwo
95 
96 // ClassTwo.test();
97 Static Hello from ClassOne - Static Hello from ClassTwo
98 
99 // again with modified static function in base class after inheritance
100 Statisches Hallo aus KlasseEins - Static Hello from ClassTwo
101 
102 // Note that not only functions are inherited
103 Two.Pi = 3.141592653589793
104

Version 23 by Lehni on 05. September 2006, 10:12

64 writeln("// again with modified function in base class after inheritance");
65 after inheritance");
80 after inheritance");

Version 22 by Lehni on 05. September 2006, 10:11

79 writeln("// again with modified static function in base class after inheritance");
80after inheritance");

Version 21 by Lehni on 05. September 2006, 10:11

19Classes are defined by using Object.extend. The function takes two hash-lists (JS Objects), the first one for the instance fields, the second one for static class fields. The static list is optional. Inheritance is implemented for both, so static class methods can be inherited from base classes and called through $super too. Each class that is created in such a way recieved the static functions "extend" and "inject". extend does the same as Object.extend, .inject works similarlysimilarly and has the same arguments, but instead of creating a new class, in it simply adds the fields to the existing class, so modification of classes at runtime is possible. If a function is overriden in such a way, $super can be used to call the previous definition of that function before the call to inject.

Version 20 by Lehni on 05. September 2006, 10:09

91 // Object.extend in Prototype, that does something different:
92 
93 // Safe our Object.extend, so it can be used in Class.create
99 
100 // Rest of Prototype follows here

Version 19 by Lehni on 05. September 2006, 10:08

90 // insert code from extend.js here, before the definition of Object.extend in Prototype, that does something differentof
91// Object.extend in Prototype, that does something different:
92
93// Safe our Object.extend, so it can be used in Class.create

Version 18 by Lehni on 05. September 2006, 10:07

86===Prototype Compatibility===
87
89 // safe Prototype's Object.insert code from extend.js here, if definition comes before this codethe definition of Object.extend in Prototype, that does something different
90 _Object_extend = Object.extend;
91 
92 // insert main code here
93 
94 // Object.extend is something different in Prototype
91 Object.extend = _Object_extend;

Version 17 by Lehni on 05. September 2006, 10:06

25Examples:===Examples===

Version 16 by Lehni on 05. September 2006, 10:03

6* Code that checks if $super is actually in use, and only wraps the function if it does use $superis.

Version 15 by Lehni on 05. September 2006, 09:59

5* The use of this.$super() instead of this.sup() (Hannes (By coincidence, Hannes named it the same, and this is a pure coincidence)same)

Version 14 by Lehni on 05. September 2006, 09:54

88 _Object_extend = Object.extend; // safe Prototype's Object.extend, if definition comes before this code
89 _Object_extend = Object.extend;
92 Object._extend = Object.extend; // Object.extend is something different in Prototype
93 Object._extend = Object.extend;

Version 13 by Lehni on 05. September 2006, 09:53

88 _Object_exstend _Object_extend = Object.extend; // safe Prototype's Object.extend, if definition comes before this code
93 Object.extend = _Object_exstend;_Object_extend;

Version 12 by Lehni on 05. September 2006, 09:52

3Hannes came up with *his own impementation|JavaScript Inheritance Sugar*, and in parallel, without realizing thisbeing aware of the feature in Hannes' version, I was following the discussion on Dean's page and found *Ben Newman's implementation|http://seraph.im/* for Prototype, which looked very clean and which I used as a base for my own improvements (described in *this post|http://helma.org/pipermail/helma-dev/2006-August/003057.html*):

Version 11 by Lehni on 05. September 2006, 09:51

9I then finally came across Hannes' implementation and realized it solves some problems in a cleaner way: For example it does not copy over methods from base class, but creates a proper inheritance chain using prototypes,. also, the His code was more readablealso less obscure, but also at the same time longer.
11I started to combine both efforts into one library and address some more issues. The result is very tight and offers a lot of functionality. I am pretty happy with it and would like decided to share it here. Bellow a short description of its the features.

Version 10 by Lehni on 05. September 2006, 09:49

13===Files===
14
16===Description===

Version 9 by Lehni on 05. September 2006, 09:48

11I started to combine both efforts into one library and address some more issues. The result is very tight and offers a lot of functionality. I am pretty happy with it and would like to share it here. Here Bellow a short description of its features, bellow the actual code.
12
13<% this.attachments %>

Version 8 by Lehni on 05. September 2006, 09:46

19Note that the constructor function's name was changed to "initialize", just like in Prototype, because "constructor" causes an error on some browsers (e.g. Mac IE).
20
22 var One ClassOne = Object.extend({ // instance fields
24  alert(write("OneHello from ClassOne");
28  alert(write('Static OneHello from ClassOne');
35 var Two ClassTwo = OneClassOne.extend({
38  alert(writeln("Two - Hello from ClassTwo");
43  alert(writeln(' - Static TwoHello from ClassTwo');
47 var o obj = new Two();ClassTwo();
49 o.test(); // One - Twowriteln();
50 writeln("// obj.test();");
51 obj.test();
52 OneClassOne.prototype.test = function() { alert("Eins");
53 write("Hallo aus KlasseEins");
54 }
54 o.test(); // Eins - Two: the change was reflect in the call to $superwriteln();
55 writeln("// again with modified function in base class after inheritance");
56 obj.test();
58 Two.test(); // Static One - Static Twowriteln();
59 writeln("// ClassTwo.test();");
60 ClassTwo.test();
61 OneClassOne.test = function() { alert("Statisch Eins");
62 write("Statisches Hallo aus KlasseEins");
63 }
63 Two.test(); // Statisch Eins - Static Two: the change was reflect in the call to $superwriteln();
64 writeln("// again with modified static function in base class after inheritance");
65 ClassTwo.test();
65 // Note that not only functions are inherited:writeln();
66 alert(Two.PI);writeln("// Note that not only functions are inherited");
67 writeln("Two.Pi = ", ClassTwo.PI);

Version 7 by Lehni on 05. September 2006, 09:22

9I then finally came across Hannes' implementation and realized it is solving solves some problems in a much cleaner way: For example it does not copy over methods from base class, but creates a proper inheritance chain using prototypes, also, the code was more readable, but also longer.
66
67With a simple addition, this code should become again compatible with Ben's prototype branch, to test it out:
68
69 _Object_exstend = Object.extend; // safe Prototype's Object.extend, if definition comes before this code
70 
71 // insert main code here
72 
73 Object._extend = Object.extend; // Object.extend is something different in Prototype
74 Object.extend = _Object_exstend;
75 
76 Class = {
77 create: function(decl, stat) {
78 return Object._extend(decl, stat);
79 }
80 }
81

Version 6 by Lehni on 05. September 2006, 09:11

13__Description__==Description==
45 
47 
49 
52 
54 
56 
58 
61 
63 

Version 5 by Lehni on 05. September 2006, 09:10

9I then finally came across Hannes' implementation and realized it is solving some problems in a much cleaner way: For example it does not copy over methods from base class, but creates a proper inheritance chain using prototypes, also, the code was more readable, but also longer.
11I started to combine both efforts into one library and address some more issues. The result is very tight and offers a lot of functionality. I am pretty happy with it and would like to share it here. Here a short description of its features, bellow the actual code.
12
13__Description__
14
15Classes are defined by using Object.extend. The function takes two hash-lists (JS Objects), the first one for the instance fields, the second one for static class fields. Inheritance is implemented for both, so static class methods can be inherited from base classes and called through $super too. Each class that is created in such a way recieved the static functions "extend" and "inject". extend does the same as Object.extend, .inject works similarly, but instead of creating a new class, in simply adds the fields to the existing class, so modification of classes at runtime is possible. If a function is overriden in such a way, $super can be used to call the previous definition of that function before the call to inject.
16
17A change was made to not "hardcode" the function if $super is used, but to look it up each time instead. Before, changes to the $super method after inheritance where not reflected in the function that inherited from the super class.
18
19Examples:
20
21 var One = Object.extend({ // instance fields
22 test: function() {
23 alert("One");
24 }
25 }, { // class fields
26 test: function() {
27 alert('Static One');
28 },
29 
30 // define a constant
31 PI: Math.PI
32 });
33 
34 var Two = One.extend({
35 test: function() {
36 this.$super();
37 alert("Two");
38 }
39 }, {
40 test: function() {
41 this.$super();
42 alert('Static Two');
43 }
44 });
45
46 var o = new Two();
47
48 o.test(); // One - Two
49
50 // now modify the function directly:
51 One.prototype.test = function() { alert("Eins"); }
52
53 o.test(); // Eins - Two: the change was reflect in the call to $super
54
55 // The same happens with static class functions:
56
57 Two.test(); // Static One - Static Two
58
59 // now modify the static function directly:
60 One.test = function() { alert("Statisch Eins"); }
61
62 Two.test(); // Statisch Eins - Static Two: the change was reflect in the call to $super
63
64 // Note that not only functions are inherited:
65 alert(Two.PI);

Version 4 by Lehni on 05. September 2006, 08:56

3Hannes came up with *his own impementation|JavaScript Inheritance Sugar*, and in parallel, without realizing this, I was following the discussion on Dean's page and found *Ben Newman's implementation|http://seraph.im/* for Prototype, which looked very clean and which I used as a base for my own improvements:improvements (described in *this post|http://helma.org/pipermail/helma-dev/2006-August/003057.html*):
7* The addition of Class.inject, that allows modifaction the modification of the class at runtime (adding of new methods and overriding existing ones by hineriting inheriting from itself).
8

Version 3 by Lehni on 05. September 2006, 08:53

3Hannes came up with *his own impementation|JavaScript Inheritance Sugar*, and in parallel, without realizing this, I was following the discussion on Dean's page and found *Ben Newman's implementation|http://seraph.im/* for Prototype, which looked very clean and which I used as a base for my own improvements:
4
5* The use of this.$super() instead of this.sup() (Hannes named it the same, and this is a pure coincidence)
6* Code that checks if $super is actually in use, and only wraps the function if it does use $super.
7* The addition of Class.inject, that allows modifaction of the class at runtime (adding of new methods and overriding existing ones by hineriting from itself).
8

Version 2 by Lehni on 05. September 2006, 08:46

2
3Hannes came up with *his own impementation|JavaScript Inheritance Sugar*

Version 1 by Lehni on 05. September 2006, 08:46

1*This post|http://helma.org/pipermail/helma-dev/2006-July/002941.html* about Dean Edward's *Base Class for JavaScript Inheritance|http://dean.edwards.name/weblog/2006/03/base/* started the discussion about JavaScript OOP in Helma.