|
| 1 | +This is basically the inheritance pattern used in Backbone so props to Jeremy Ashkenas and the Google Closure team: |
1 | 2 |
|
2 |
| -# JavaScript OO |
| 3 | +* Pseudoclassical inheritence (see [this blog post](http://bolinfest.com/javascript/inheritance.php) for pros/cons of this approach). |
| 4 | +* Ability to extend both prototype and Class methods. |
| 5 | +* Ability to override the constructor. |
3 | 6 |
|
4 |
| -Super lightweight Object Oriented library for JavaScript. |
| 7 | +In addition, this script adds: |
5 | 8 |
|
6 |
| -## Examples |
7 |
| - |
8 |
| -Create a simple class, with no properties or methods: |
9 |
| - |
10 |
| - User = Class() |
11 |
| - // or Class.extend() |
12 |
| - |
13 |
| -Create a class containing both properties and methods: |
14 |
| - |
15 |
| - User = Class({ |
16 |
| - type: 'user', |
17 |
| - init: function(name) { |
18 |
| - this.name = name |
19 |
| - } |
20 |
| - }) |
21 |
| - |
22 |
| -Create an instance of the "User" class above: |
23 |
| - |
24 |
| - tj = new User('tj') |
25 |
| - tj.type |
26 |
| - // => "user" |
27 |
| - tj.name |
28 |
| - // => "tj" |
29 |
| - |
30 |
| -Subclass the User class we created: |
31 |
| - |
32 |
| - Admin = User.extend({ |
33 |
| - type: 'admin' |
34 |
| - }) |
35 |
| - |
36 |
| - tj = new Admin('tj') |
37 |
| - tj.type |
38 |
| - // => "admin" |
39 |
| - tj.name |
40 |
| - // => "tj" |
41 |
| - |
42 |
| -Reference superclass methods using __super__: |
43 |
| - |
44 |
| - Admin = User.extend({ |
45 |
| - init: function(name, age) { |
46 |
| - this.__super__(name) |
47 |
| - this.age = age |
48 |
| - }, |
49 |
| - |
50 |
| - toString: function() { |
51 |
| - return '<Admin name="' + this.name + '" age=' + this.age + '>' |
52 |
| - } |
53 |
| - }) |
54 |
| - |
55 |
| - tj = new Admin('tj', 22) |
56 |
| - tj.toString() |
57 |
| - // => "<Admin name=\"tj\" age=22>" |
58 |
| - |
59 |
| -Include more methods: |
60 |
| - |
61 |
| - Admin.include({ |
62 |
| - says: function(){ |
63 |
| - return 'Im an admin!' |
64 |
| - } |
65 |
| - }) |
66 |
| - |
67 |
| -Or include some mixins (objects): |
68 |
| - |
69 |
| - Enumerable = { ... } |
70 |
| - Comparable = { ... } |
71 |
| - |
72 |
| - Animal = Class({ |
73 |
| - include: [Enumerable, Comparable] |
74 |
| - }) |
75 |
| - |
76 |
| -Or a single mixin: |
77 |
| - |
78 |
| - Animal = Class({ |
79 |
| - include: Enumerable |
80 |
| - }) |
81 |
| - |
82 |
| -Extend singleton prototype with methods: |
83 |
| - |
84 |
| - User = Class({ |
85 |
| - extend: { |
86 |
| - path: '/users' |
87 |
| - } |
88 |
| - }) |
89 |
| - |
90 |
| - User.path |
91 |
| - // => '/users' |
92 |
| - |
93 |
| -Or using "mixins": |
94 |
| - |
95 |
| - User = Class({ |
96 |
| - extend: Foo |
97 |
| - }) |
98 |
| - |
99 |
| -Or using several "mixins": |
100 |
| - |
101 |
| - User = Class({ |
102 |
| - extend: [Foo, Bar] |
103 |
| - }) |
104 |
| - |
105 |
| -## License |
106 |
| - |
107 |
| -(The MIT License) |
108 |
| - |
109 |
| -Copyright (c) 2009 TJ Holowaychuk <[email protected]> |
110 |
| - |
111 |
| -Permission is hereby granted, free of charge, to any person obtaining |
112 |
| -a copy of this software and associated documentation files (the |
113 |
| -'Software'), to deal in the Software without restriction, including |
114 |
| -without limitation the rights to use, copy, modify, merge, publish, |
115 |
| -distribute, sublicense, and/or sell copies of the Software, and to |
116 |
| -permit persons to whom the Software is furnished to do so, subject to |
117 |
| -the following conditions: |
118 |
| - |
119 |
| -The above copyright notice and this permission notice shall be |
120 |
| -included in all copies or substantial portions of the Software. |
121 |
| - |
122 |
| -THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
123 |
| -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
124 |
| -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
125 |
| -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
126 |
| -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
127 |
| -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
128 |
| -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 9 | +* No 3rd-party dependencies. |
| 10 | +* Built in support for calling `initialize` by the constructor. |
| 11 | +* Will automatically extend simple objects, rather than replace them (useful for `options`, etc). |
| 12 | +* Adds __name__ property to methods for introspection (ie for use with a call stack - see specs for usage). |
0 commit comments