Permalink
Browse files

add a note, inherit from Stream

  • Loading branch information...
1 parent c205cbc commit 325fa2689ae134c84f595068ddc657122c95f090 @substack committed Sep 1, 2012
Showing with 54 additions and 4 deletions.
  1. +11 −0 example/f.js
  2. +13 −0 example/foo/index.js
  3. +9 −0 example/xs.js
  4. +13 −0 index.js
  5. +8 −4 readme.markdown
View
@@ -0,0 +1,11 @@
+var funstance = require('../');
+var Foo = require('./foo');
+
+var obj = new Foo(4);
+var fobj = funstance(obj, function (n) {
+ return n * obj.x
+});
+
+console.log(fobj(111));
+fobj.on('beep', console.log);
+fobj.beep();
View
@@ -0,0 +1,13 @@
+var inherits = require('inherits');
+var Stream = require('stream');
+
+module.exports = Foo;
+inherits(Foo, Stream);
+
+function Foo (x) {
+ this.x = x;
+}
+
+Foo.prototype.beep = function () {
+ this.emit('beep', 'boop');
+};
View
@@ -0,0 +1,9 @@
+var funstance = require('../');
+var xs = [1,2,3];
+var fxs = funstance(xs, function (n) { return this.push(n) });
+
+console.log(fxs.length);
+console.log(fxs.shift());
+
+fxs(555);
+console.log(fxs.join(','));
View
@@ -0,0 +1,13 @@
+module.exports = function (obj, fn) {
+ var f = function () {
+ if (typeof fn !== 'function') return;
+ return fn.apply(obj, arguments);
+ };
+ f.__proto__ = obj.__proto__;
+
+ Object.getOwnPropertyNames(obj).forEach(function (key) {
+ f[key] = obj[key];
+ });
+
+ return f;
+};
View
@@ -7,7 +7,8 @@ make an instance callable like a function
## funstantiate an array
``` js
-var xs = [1,2,3]
+var funstance = require('funstance');
+var xs = [1,2,3];
var fxs = funstance(xs, function (n) { return this.push(n) });
console.log(fxs.length);
@@ -28,13 +29,14 @@ $ node xs.js
## funstantiate a prototypical object
-suppose
+foo.js:
``` js
var inherits = require('inherits');
-var EventEmitter = require('events').EventEmitter;
+var Stream = require('stream');
module.exports = Foo;
+inherits(Foo, Stream);
function Foo (x) {
this.x = x;
@@ -61,7 +63,9 @@ fobj.on('beep', console.log);
fobj.beep();
```
-***
+Note that `.on()` is defined all the way in EventEmitter, which is 3 times
+removed up the prototype chain from `fobj`, yet `fobj.on()` still works despite
+being a function.
```
$ node main.js

0 comments on commit 325fa26

Please sign in to comment.