You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-4Lines changed: 46 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# augment #
2
2
3
-
The world's smallest and fastest classical JavaScript inheritance pattern (`Function.prototype.augment`) is an [eight line function](https://github.com/javascript/augment/blob/master/lib/augment.js#L18"augment.js") which allows you to write [CoffeeScript style classes](http://coffeescript.org/#classes"CoffeeScript") with a flair of [simplicity](http://ejohn.org/blog/simple-javascript-inheritance/"John Resig - Simple JavaScript Inheritance"); and it still [beats the bejesus](http://jsperf.com/oop-benchmark/118"JavaScript Object Oriented Libraries Benchmark · jsPerf") out of other JavaScript inheritance libraries.
3
+
The world's smallest and fastest classical JavaScript inheritance pattern (`augment`) is a [seven line function](https://github.com/javascript/augment/blob/master/lib/augment.js#L13"augment.js") which allows you to write [CoffeeScript style classes](http://coffeescript.org/#classes"CoffeeScript") with a flair of [simplicity](http://ejohn.org/blog/simple-javascript-inheritance/"John Resig - Simple JavaScript Inheritance"); and it still [beats the bejesus](http://jsperf.com/oop-benchmark/118"JavaScript Object Oriented Libraries Benchmark · jsPerf") out of other JavaScript inheritance libraries.
4
4
5
5
Inspired by giants like [Jeremy Ashkenas](http://ashkenas.com/"Jeremy/Ashkenas — Portfolio") and [John Resig](http://ejohn.org/"John Resig - JavaScript Programmer"), `augment` is an augmentation of ideas. Classes created using `augment` have a CoffeeScript like structure, and a syntax like John Resig's; but they are more readable, intuitive and orders of magnitude faster. Plus they work on every JavaScript platform.
6
6
@@ -12,10 +12,10 @@ You can install `augment` on [node.js](http://nodejs.org/ "node.js") using the f
12
12
npm install augment
13
13
```
14
14
15
-
You can also install `augment` on [RingoJS](http://ringojs.org/"Home - RingoJS") using the following `ringo-admin` command:
15
+
You can also install `augment` on [RingoJS](http://ringojs.org/"Home - RingoJS") using the following [rp](https://github.com/grob/rp"grob/rp") command:
16
16
17
17
```bash
18
-
ringo-admin install javascript/augment
18
+
rp install augment
19
19
```
20
20
21
21
Similarly you can install `augment` for web apps using the following [component](https://github.com/component/component) command:
@@ -37,6 +37,42 @@ Otherwise you may simply browse the [source code](https://github.com/javascript/
37
37
I am a huge follower of keeping things simple and learning by example. So let's begin:
38
38
39
39
```javascript
40
+
var augment =require("augment");
41
+
42
+
var Rectangle =augment(Object, function () {
43
+
this.constructor=function (width, height) {
44
+
this.height= height;
45
+
this.width= width;
46
+
};
47
+
48
+
this.area=function () {
49
+
returnthis.width*this.height;
50
+
};
51
+
});
52
+
```
53
+
54
+
The `augment` function augments objects. However if you pass it a function as the first parameter instead then it augments the `prototype` of the function. It's equivalent to:
55
+
56
+
```javascript
57
+
var augment =require("augment");
58
+
59
+
var Rectangle =augment(Object.prototype, function () {
60
+
this.constructor=function (width, height) {
61
+
this.height= height;
62
+
this.width= width;
63
+
};
64
+
65
+
this.area=function () {
66
+
returnthis.width*this.height;
67
+
};
68
+
});
69
+
```
70
+
71
+
The `augment` function is also available as a method on `Object.prototype` and `Function.prototype` allowing you to alternatively write the above code as follows:
72
+
73
+
```javascript
74
+
var augment =require("augment");
75
+
40
76
var Rectangle =Object.augment(function () {
41
77
this.constructor=function (width, height) {
42
78
this.height= height;
@@ -156,6 +192,8 @@ deferredAlert("This will be displayed later.");
156
192
alert("This will be displayed first.");
157
193
```
158
194
195
+
The `bindable` function is equivalent to `bind.bind` (i.e. it takes a function `foo` and returns another function equivalent to `foo.bind`, or to put it simply `bindable(foo) === foo.bind`). Hence `bindable(bind)` is equivalent to `bind.bind` or `bindable` itself. In fact `bindable` is itself is implemented as `bind.bind(bind)`. Confusing? I think not.
196
+
159
197
As a thumb rule the name of the bindable version of a function should be an adjective with the suffix _"able"_. For example a bindable `bind` would be `bindable` itself (which is what it actually is). A bindable `call` would be `callable`. A bindable `apply` would be `appliable`. You get my drift. Concise and descriptive names are very helpful.
160
198
161
199
### Function.callable ###
@@ -177,6 +215,10 @@ The `Array.from` function allows you to slice an array from a start index to an
177
215
```javascript
178
216
var primes = [2, 3, 5, 7];
179
217
var oddPrimes =tail(primes); // [3, 5, 7]
218
+
219
+
functiontail(list) {
220
+
returnarrayFrom(list, 1);
221
+
}
180
222
```
181
223
182
224
### Object.ownPropertyOf ###
@@ -188,4 +230,4 @@ var object = Object.create(null);
0 commit comments