Skip to content

Commit a4b1690

Browse files
author
Aadit M Shah
committed
Bumped to version 3.3 - fixes aaditmshah#2 fixes aaditmshah#3
1 parent 6f64137 commit a4b1690

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# augment #
22

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.
44

55
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.
66

@@ -12,10 +12,10 @@ You can install `augment` on [node.js](http://nodejs.org/ "node.js") using the f
1212
npm install augment
1313
```
1414

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:
1616

1717
```bash
18-
ringo-admin install javascript/augment
18+
rp install augment
1919
```
2020

2121
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/
3737
I am a huge follower of keeping things simple and learning by example. So let's begin:
3838

3939
```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+
return this.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+
return this.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+
4076
var Rectangle = Object.augment(function () {
4177
this.constructor = function (width, height) {
4278
this.height = height;
@@ -156,6 +192,8 @@ deferredAlert("This will be displayed later.");
156192
alert("This will be displayed first.");
157193
```
158194

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+
159197
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.
160198

161199
### Function.callable ###
@@ -177,6 +215,10 @@ The `Array.from` function allows you to slice an array from a start index to an
177215
```javascript
178216
var primes = [2, 3, 5, 7];
179217
var oddPrimes = tail(primes); // [3, 5, 7]
218+
219+
function tail(list) {
220+
return arrayFrom(list, 1);
221+
}
180222
```
181223

182224
### Object.ownPropertyOf ###
@@ -188,4 +230,4 @@ var object = Object.create(null);
188230
Object.ownPropertyOf(object, "property"); // false
189231
```
190232

191-
That's all folks!
233+
That's all folks!

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "augment",
33
"repo": "javascript/augment",
44
"description": "The world's smallest and fastest classical JavaScript inheritance pattern.",
5-
"version": "3.2.1",
5+
"version": "3.3.0",
66
"keywords": ["augment", "augments", "augmentation", "extend", "extends", "extension", "prototype", "prototypal", "class", "classical", "object", "inheritance", "uber", "super", "constructor", "oop"],
77
"main": "lib/augment.js",
88
"scripts": ["lib/augment.js"],

lib/augment.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
Function.prototype.augment = function (body) {
2-
var base = this.prototype;
3-
var prototype = Object.create(base);
4-
body.apply(prototype, Array.from(arguments, 1).concat(base));
5-
if (!Object.ownPropertyOf(prototype, "constructor")) return prototype;
6-
var constructor = prototype.constructor;
7-
constructor.prototype = prototype;
8-
return constructor;
9-
};
10-
11-
(function (functProto) {
12-
var bind = functProto.bind;
1+
var augment = (function (bind, call) {
132
var bindable = Function.bindable = bind.bind(bind);
14-
var callable = Function.callable = bindable(functProto.call);
15-
Object.ownPropertyOf = callable(Object.prototype.hasOwnProperty);
16-
Array.from = callable(Array.prototype.slice);
17-
}(Function.prototype));
3+
var callable = Function.callable = bindable(call);
4+
5+
var arrayFrom = Array.from = callable(Array.prototype.slice);
6+
var ownPropertyOf = Object.ownPropertyOf = callable(Object.hasOwnProperty);
7+
8+
Function.prototype.augment = augment;
9+
Object.prototype.augment = augment;
10+
11+
return callable(augment);
12+
13+
function augment(body) {
14+
var base = this.prototype || this;
15+
var prototype = Object.create(base);
16+
body.apply(prototype, arrayFrom(arguments, 1).concat(base));
17+
if (!ownPropertyOf(prototype, "constructor")) return prototype;
18+
var constructor = prototype.constructor;
19+
constructor.prototype = prototype;
20+
return constructor;
21+
}
22+
}(Function.bind, Function.call));
23+
24+
if (typeof module === "object") module.exports = augment;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "augment",
33
"description": "The world's smallest and fastest classical JavaScript inheritance pattern.",
4-
"version": "3.2.1",
4+
"version": "3.3.0",
55
"keywords": ["augment", "augments", "augmentation", "extend", "extends", "extension", "prototype", "prototypal", "class", "classical", "object", "inheritance", "uber", "super", "constructor", "oop"],
66
"author": "Aadit M Shah (http://aaditmshah.github.com/) <[email protected]>",
77
"main": "lib/augment.js",

0 commit comments

Comments
 (0)