8000 Add quick functor implementation · HowProgrammingWorks/Prototype@859b468 · GitHub
[go: up one dir, main page]

Skip to content

Commit 859b468

Browse files
committed
Add quick functor implementation
1 parent c7c0091 commit 859b468

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

JavaScript/f-functor.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
5+
function Functor() {
6+
}
7+
8+
Functor.prototype.method = function() {
9+
console.log('Functor.prototype.method');
10+
console.dir({
11+
property: this.property,
12+
exec: this.method
13+
});
14+
this(); // you can call this as a function
15+
};
16+
17+
util.inherits(Functor, Function);
18+
19+
const factory = () => {
20+
21+
const functor = function() {
22+
console.log('functor');
23+
functor.property = 'value';
24+
console.dir({
25+
property: functor.property,
26+
method: functor.method
27+
});
28+
};
29+
30+
Object.setPrototypeOf(functor, Functor.prototype);
31+
return functor;
32+
};
33+
34+
// Usage
35+
36+
const fn = factory();
37+
fn();
38+
fn.method();

0 commit comments

Comments
 (0)
0