summaryrefslogtreecommitdiff
path: root/lib/Evolving/evolving.litcoffee
blob: 2e06aa8a7f4c7bab8f41c71a2ab4a8a8181f2f05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'Evolving' - adaptive object evolution.  Soar above classes, prototypes and mixins

    ###
    
    Copyright (c) 2015 Michele Bini


    This library is free software: you can redistribute it and/or modify
    it under the terms of the version 3 of the GNU General Public License
    as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ###



Define 'Evolving' as a generic base class

    class Evolving


Define the 'evolve' method, that applies 'evolution' to the current class.

It returns an ordinary Javascript class you can instantiate with 'new'.

Technically, since Javascript objects have prototypal inheritance, "classes" are just constructors.

      evolve: (evolution)->
          x = ->
          x.prototype = @


The object generated by `new` may have the constructor set to x.prototype.constructor, instead of x.   So, we set the constructor right back to x. 
  
          (y = new x).constructor = x


Set defaults for the new class:

          x.evolution = evolution
          x.prototype = y


Finally let the evolution happen, and return the new class

          evolution.call(y).constructor


Export 'Evolving' if necessary

    exports?.Evolving = Evolving